func (s *FailIfImportedLibraryIsWrong) Run(context map[string]interface{}) error {
	if !utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) {
		return nil
	}

	logger := context[constants.CTX_LOGGER].(i18n.Logger)
	importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)

	for _, library := range importedLibraries {
		if !library.IsLegacy {
			if stat, err := os.Stat(filepath.Join(library.Folder, constants.LIBRARY_FOLDER_ARCH)); err == nil && stat.IsDir() {
				return utils.ErrorfWithLogger(logger, constants.MSG_ARCH_FOLDER_NOT_SUPPORTED)
			}
			for _, propName := range LIBRARY_MANDATORY_PROPERTIES {
				if _, ok := library.Properties[propName]; !ok {
					return utils.ErrorfWithLogger(logger, constants.MSG_PROP_IN_LIBRARY, propName, library.Folder)
				}
			}
			if library.Layout == types.LIBRARY_RECURSIVE {
				if stat, err := os.Stat(filepath.Join(library.Folder, constants.LIBRARY_FOLDER_UTILITY)); err == nil && stat.IsDir() {
					return utils.ErrorfWithLogger(logger, constants.MSG_LIBRARY_CAN_USE_SRC_AND_UTILITY_FOLDERS, library.Folder)
				}
			}
		}
	}

	return nil
}
Exemple #2
0
func PrepareCommandForRecipe(properties map[string]string, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (*exec.Cmd, error) {
	pattern := properties[recipe]
	if pattern == constants.EMPTY_STRING {
		return nil, utils.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
	}

	var err error
	commandLine := props.ExpandPropsInString(properties, pattern)
	if removeUnsetProperties {
		commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine)
		if err != nil {
			return nil, utils.WrapError(err)
		}
	}

	command, err := utils.PrepareCommand(commandLine, logger)
	if err != nil {
		return nil, utils.WrapError(err)
	}

	if echoCommandLine {
		fmt.Println(commandLine)
	}

	return command, nil
}
func makeSketch(sketchLocation string, allSketchFilePaths []string, logger i18n.Logger) (*types.Sketch, error) {
	sketchFilesMap := make(map[string]types.SketchFile)
	for _, sketchFilePath := range allSketchFilePaths {
		source, err := ioutil.ReadFile(sketchFilePath)
		if err != nil {
			return nil, utils.WrapError(err)
		}
		sketchFilesMap[sketchFilePath] = types.SketchFile{Name: sketchFilePath, Source: string(source)}
	}

	mainFile := sketchFilesMap[sketchLocation]
	delete(sketchFilesMap, sketchLocation)

	additionalFiles := []types.SketchFile{}
	otherSketchFiles := []types.SketchFile{}
	mainFileDir := filepath.Dir(mainFile.Name)
	for _, sketchFile := range sketchFilesMap {
		ext := strings.ToLower(filepath.Ext(sketchFile.Name))
		if MAIN_FILE_VALID_EXTENSIONS[ext] {
			if filepath.Dir(sketchFile.Name) == mainFileDir {
				otherSketchFiles = append(otherSketchFiles, sketchFile)
			}
		} else if ADDITIONAL_FILE_VALID_EXTENSIONS[ext] {
			additionalFiles = append(additionalFiles, sketchFile)
		} else {
			return nil, utils.ErrorfWithLogger(logger, constants.MSG_UNKNOWN_SKETCH_EXT, sketchFile.Name)
		}
	}

	sort.Sort(types.SketchFileSortByName(additionalFiles))
	sort.Sort(types.SketchFileSortByName(otherSketchFiles))

	return &types.Sketch{MainFile: mainFile, OtherSketchFiles: otherSketchFiles, AdditionalFiles: additionalFiles}, nil
}
func LoadFromSlice(lines []string, logger i18n.Logger) (PropertiesMap, error) {
	properties := make(PropertiesMap)

	for _, line := range lines {
		err := properties.loadSingleLine(line)
		if err != nil {
			return nil, utils.ErrorfWithLogger(logger, constants.MSG_WRONG_PROPERTIES, line)
		}
	}

	return properties, nil
}
func Load(filepath string, logger i18n.Logger) (PropertiesMap, error) {
	bytes, err := ioutil.ReadFile(filepath)
	if err != nil {
		return nil, utils.WrapError(err)
	}

	text := string(bytes)
	text = strings.Replace(text, "\r\n", "\n", -1)
	text = strings.Replace(text, "\r", "\n", -1)

	properties := make(PropertiesMap)

	for _, line := range strings.Split(text, "\n") {
		err := properties.loadSingleLine(line)
		if err != nil {
			return nil, utils.ErrorfWithLogger(logger, constants.MSG_WRONG_PROPERTIES_FILE, line, filepath)
		}
	}

	return properties, nil
}
func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*types.Library, error) {
	properties, err := props.Load(filepath.Join(libraryFolder, constants.LIBRARY_PROPERTIES), logger)
	if err != nil {
		return nil, utils.WrapError(err)
	}

	if properties[constants.LIBRARY_MAINTAINER] == constants.EMPTY_STRING && properties[constants.LIBRARY_EMAIL] != constants.EMPTY_STRING {
		properties[constants.LIBRARY_MAINTAINER] = properties[constants.LIBRARY_EMAIL]
	}

	if stat, err := os.Stat(filepath.Join(libraryFolder, constants.LIBRARY_FOLDER_ARCH)); err == nil && stat.IsDir() {
		return nil, utils.ErrorfWithLogger(logger, constants.MSG_ARCH_FOLDER_NOT_SUPPORTED)
	}

	for _, propName := range LIBRARY_MANDATORY_PROPERTIES {
		if _, ok := properties[propName]; !ok {
			return nil, utils.ErrorfWithLogger(logger, constants.MSG_PROP_IN_LIBRARY, propName, libraryFolder)
		}
	}
	for _, propName := range LIBRARY_NOT_SO_MANDATORY_PROPERTIES {
		if properties[propName] == constants.EMPTY_STRING {
			properties[propName] = "-"
		}
	}

	library := &types.Library{}
	if stat, err := os.Stat(filepath.Join(libraryFolder, constants.LIBRARY_FOLDER_SRC)); err == nil && stat.IsDir() {
		library.Layout = types.LIBRARY_RECURSIVE
		library.SrcFolder = filepath.Join(libraryFolder, constants.LIBRARY_FOLDER_SRC)
		if stat, err := os.Stat(filepath.Join(libraryFolder, constants.LIBRARY_FOLDER_UTILITY)); err == nil && stat.IsDir() {
			return nil, utils.ErrorfWithLogger(logger, constants.MSG_LIBRARY_CAN_USE_SRC_AND_UTILITY_FOLDERS)
		}
	} else {
		library.Layout = types.LIBRARY_FLAT
		library.SrcFolder = libraryFolder
	}

	subFolders, err := utils.ReadDirFiltered(libraryFolder, utils.FilterDirs)
	if err != nil {
		return nil, utils.WrapError(err)
	}

	if debugLevel > 0 {
		for _, subFolder := range subFolders {
			if utils.IsSCCSOrHiddenFile(subFolder) {
				logger.Fprintln(os.Stderr, constants.MSG_WARNING_SPURIOUS_FILE_IN_LIB, filepath.Base(subFolder.Name()), properties[constants.LIBRARY_NAME])
			}
		}
	}

	if properties[constants.LIBRARY_ARCHITECTURES] == constants.EMPTY_STRING {
		properties[constants.LIBRARY_ARCHITECTURES] = constants.LIBRARY_ALL_ARCHS
	}
	library.Archs = []string{}
	for _, arch := range strings.Split(properties[constants.LIBRARY_ARCHITECTURES], ",") {
		library.Archs = append(library.Archs, strings.TrimSpace(arch))
	}

	properties[constants.LIBRARY_CATEGORY] = strings.TrimSpace(properties[constants.LIBRARY_CATEGORY])
	if !LIBRARY_CATEGORIES[properties[constants.LIBRARY_CATEGORY]] {
		logger.Fprintln(os.Stderr, constants.MSG_WARNING_LIB_INVALID_CATEGORY, properties[constants.LIBRARY_CATEGORY], properties[constants.LIBRARY_NAME], constants.LIB_CATEGORY_UNCATEGORIZED)
		properties[constants.LIBRARY_CATEGORY] = constants.LIB_CATEGORY_UNCATEGORIZED
	}
	library.Category = properties[constants.LIBRARY_CATEGORY]

	if properties[constants.LIBRARY_LICENSE] == constants.EMPTY_STRING {
		properties[constants.LIBRARY_LICENSE] = constants.LIB_LICENSE_UNSPECIFIED
	}
	library.License = properties[constants.LIBRARY_LICENSE]

	library.Folder = libraryFolder
	library.Name = filepath.Base(libraryFolder)
	library.Version = strings.TrimSpace(properties[constants.LIBRARY_VERSION])
	library.Author = strings.TrimSpace(properties[constants.LIBRARY_AUTHOR])
	library.Maintainer = strings.TrimSpace(properties[constants.LIBRARY_MAINTAINER])
	library.Sentence = strings.TrimSpace(properties[constants.LIBRARY_SENTENCE])
	library.Paragraph = strings.TrimSpace(properties[constants.LIBRARY_PARAGRAPH])
	library.URL = strings.TrimSpace(properties[constants.LIBRARY_URL])
	library.IsLegacy = false
	library.DotALinkage = strings.TrimSpace(properties[constants.LIBRARY_DOT_A_LINKAGE]) == "true"

	return library, nil
}