func (s *HardwareLoader) Run(context map[string]interface{}) error { mainHardwarePlatformTxt := make(map[string]string) packages := make(map[string]*types.Package) folders := context[constants.CTX_HARDWARE_FOLDERS].([]string) folders, err := utils.AbsolutizePaths(folders) if err != nil { return utils.WrapError(err) } for _, folder := range folders { stat, err := os.Stat(folder) if err != nil { return utils.WrapError(err) } if !stat.IsDir() { return utils.Errorf(context, constants.MSG_MUST_BE_A_FOLDER, folder) } if len(mainHardwarePlatformTxt) == 0 { mainHardwarePlatformTxt, err = props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT)) if err != nil { return utils.WrapError(err) } } hardwarePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT)) if err != nil { return utils.WrapError(err) } hardwarePlatformTxt = utils.MergeMapsOfStrings(make(map[string]string), mainHardwarePlatformTxt, hardwarePlatformTxt) subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs) if err != nil { return utils.WrapError(err) } subfolders = utils.FilterOutFoldersByNames(subfolders, constants.FOLDER_TOOLS) for _, subfolder := range subfolders { subfolderPath := filepath.Join(folder, subfolder.Name()) packageId := subfolder.Name() if _, err := os.Stat(filepath.Join(subfolderPath, constants.FOLDER_HARDWARE)); err == nil { subfolderPath = filepath.Join(subfolderPath, constants.FOLDER_HARDWARE) } targetPackage := getOrCreatePackage(packages, packageId) err = loadPackage(targetPackage, subfolderPath, hardwarePlatformTxt) if err != nil { return utils.WrapError(err) } packages[packageId] = targetPackage } } context[constants.CTX_HARDWARE] = packages return nil }
func (s *HardwareLoader) Run(ctx *types.Context) error { logger := ctx.GetLogger() packages := &types.Packages{} packages.Packages = make(map[string]*types.Package) packages.Properties = make(map[string]string) folders := ctx.HardwareFolders folders, err := utils.AbsolutizePaths(folders) if err != nil { return i18n.WrapError(err) } for _, folder := range folders { stat, err := os.Stat(folder) if err != nil { return i18n.WrapError(err) } if !stat.IsDir() { return i18n.ErrorfWithLogger(logger, constants.MSG_MUST_BE_A_FOLDER, folder) } hardwarePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger) if err != nil { return i18n.WrapError(err) } packages.Properties.Merge(hardwarePlatformTxt) subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs) if err != nil { return i18n.WrapError(err) } subfolders = utils.FilterOutFoldersByNames(subfolders, constants.FOLDER_TOOLS) for _, subfolder := range subfolders { subfolderPath := filepath.Join(folder, subfolder.Name()) packageId := subfolder.Name() if _, err := os.Stat(filepath.Join(subfolderPath, constants.FOLDER_HARDWARE)); err == nil { subfolderPath = filepath.Join(subfolderPath, constants.FOLDER_HARDWARE) } targetPackage := getOrCreatePackage(packages, packageId) err = loadPackage(targetPackage, subfolderPath, logger) if err != nil { return i18n.WrapError(err) } packages.Packages[packageId] = targetPackage } } ctx.Hardware = packages return nil }
func loadPackage(targetPackage *types.Package, folder string, logger i18n.Logger) error { packagePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger) if err != nil { return utils.WrapError(err) } targetPackage.Properties = utils.MergeMapsOfStrings(targetPackage.Properties, packagePlatformTxt) subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs) if err != nil { return utils.WrapError(err) } subfolders = utils.FilterOutFoldersByNames(subfolders, constants.FOLDER_TOOLS) platforms := targetPackage.Platforms for _, subfolder := range subfolders { subfolderPath := filepath.Join(folder, subfolder.Name()) platformId := subfolder.Name() _, err := os.Stat(filepath.Join(subfolderPath, constants.FILE_BOARDS_TXT)) if err != nil && os.IsNotExist(err) { theOnlySubfolder, err := utils.TheOnlySubfolderOf(subfolderPath) if err != nil { return utils.WrapError(err) } if theOnlySubfolder != constants.EMPTY_STRING { subfolderPath = filepath.Join(subfolderPath, theOnlySubfolder) } } platform := getOrCreatePlatform(platforms, platformId) err = loadPlatform(platform, targetPackage.PackageId, subfolderPath, logger) if err != nil { return utils.WrapError(err) } platforms[platformId] = platform } return nil }