Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/fetch/cache_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const (
type CacheStrategy int

// determinePackageCacheStrategy determines package cache strategy via package filesystem path.
func determinePackageCacheStrategy(packageMeta pkg.PackageMeta, projectRoot string) (error, CacheStrategy) {
// noCache: dont use the global cache
func determinePackageCacheStrategy(packageMeta pkg.PackageMeta, projectRoot string, noCache bool) (error, CacheStrategy) {
pkgCachePath := packageMeta.HomeCacheSrcPath() // package global cache dir
pkgVendorPath := packageMeta.VendorSrcPath(projectRoot)

Expand All @@ -28,7 +29,10 @@ func determinePackageCacheStrategy(packageMeta pkg.PackageMeta, projectRoot stri
return nil, CacheStrategyDownloadFromRemote
}
return err, CacheStrategySkip
} else { // vendor src does not exist, but global cache exist.
} else { // vendor src does not exist, but global cache exist.
if noCache { // if noCache, directly download. Don't use global cache.
return err, CacheStrategyDownloadFromRemote
}
return nil, CacheStrategyCopyFromGlobalCache
}
} else if err != nil { // other error for vendor src stat
Expand Down
4 changes: 3 additions & 1 deletion pkg/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
fetchCommand.FlagSet.StringVar(&f.PkgHome, "p", pwd, "absolute or relative path for file "+pkg.PkgFileName)
fetchCommand.FlagSet.StringVar(&f.CMakeFindPackageOption, "cmake-find-package-arg", "NO_DEFAULT_PATH", "global options for find_package when generating file pkg.dep.cmake")
fetchCommand.FlagSet.StringVar(&f.FeaturesOption, "features", DefaultFeatureName, "Comma separated list of features to activate. e.g. --features=foo,bar")
fetchCommand.FlagSet.BoolVar(&f.NoCache, "no-cache", false, "Don't use the system cache. Directly download from the Internet")
// todo make pkgHome abs path anyway.
fetchCommand.FlagSet.Usage = fetchCommand.Usage // use default usage provided by cmds.Command.
fetchCommand.Runner = &f
Expand All @@ -55,6 +56,7 @@ type fetch struct {
FeaturesOption string // cli `feature` string
FeatureList []string // feature list parsed from cli option.
MirrorConfPath string // the file path of repo mirror file.
NoCache bool // download package without using global cache
DepTree pkg.DependencyTree
Auth map[string]conf.Auth
GlobalReplace map[string]string
Expand Down Expand Up @@ -329,7 +331,7 @@ func (f *fetch) dlPackagesDepSrc(pkgLock *map[string]string, featPkgList []strin
srcDes := context.HomeCacheSrcPath()
vendorSrcDes := context.VendorSrcPath(f.PkgHome)

err, strategy := determinePackageCacheStrategy(context, f.PkgHome)
err, strategy := determinePackageCacheStrategy(context, f.PkgHome, f.NoCache)
if err != nil {
return nil, err
}
Expand Down
49 changes: 29 additions & 20 deletions pkg/fetch/src_dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ func filesSrc(srcDes, packageName, baseUrl string, files map[string]string) erro
}
}

// move dir from temp dir to real source file location.
log.WithFields(log.Fields{"pkg": packageName, "temp path": tempPath, "src path": srcDes}).
Debugln("move dependency from temporary directory to source path.")
if err := os.Rename(tempPath, srcDes); err != nil {
// move dir from temp dir to real source file location in postDownloadStep.
if err := postDownloadStep(packageName, tempPath, srcDes); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -215,10 +213,8 @@ func archiveSrc(archiveType string, srcPath string, packageName string, remoteUr
"storage": tempPath,
}).Println("finished extracting package.")

// move dir from temp dir to real source file location.
log.WithFields(log.Fields{"pkg": packageName, "temp path": tempPath, "src path": srcPath}).
Debugln("move dependency from temporary directory to source path.")
if err := os.Rename(tempPath, srcPath); err != nil {
// move dir from temp dir to real source file location in postDownloadStep.
if err := postDownloadStep(packageName, tempPath, srcPath); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -326,19 +322,8 @@ func gitSrc(auths map[string]conf.Auth, packageCacheDir, packagePath, packageUrl
return err
}

// move temp dir to global home source dir
log.WithFields(log.Fields{"pkg": packagePath, "temp path": tempPath, "src path": packageCacheDir}).
Debugln("move dependency from temporary directory to source path.")
// create parent dir first and then perform move.
if srcParentDir, err := pkg.GetCachedPackageSrcPath(packagePath, ".draft"); err != nil {
if err := postDownloadStep(packagePath, tempPath, packageCacheDir); err != nil {
return err
} else {
if err := os.MkdirAll(srcParentDir, 0744); err != nil {
return err
}
if err := os.Rename(tempPath, packageCacheDir); err != nil {
return err
}
}
}

Expand All @@ -349,3 +334,27 @@ func gitSrc(auths map[string]conf.Auth, packageCacheDir, packagePath, packageUrl

return nil
}

func postDownloadStep(packageName, tempPath, packageCacheDir string) error {
// move temp dir to global home source dir
log.WithFields(log.Fields{"pkg": packageName, "temp path": tempPath, "src path": packageCacheDir}).
Debugln("move dependency from temporary directory to source path.")
// create parent dir first and then perform move.
if srcParentDir, err := pkg.GetCachedPackageSrcPath(packageName, ".draft"); err != nil {
return err
} else {
if err := os.MkdirAll(srcParentDir, 0744); err != nil {
return err
}
// remove the old package directory if possible
if err := os.RemoveAll(packageCacheDir); err != nil {
return err
}

// perform renaming: move from tem dir to the system cache dir.
if err := os.Rename(tempPath, packageCacheDir); err != nil {
return err
}
}
return nil
}