Skip to content

Commit

Permalink
Merge pull request #53 from bitrise-io/follow-symlinks
Browse files Browse the repository at this point in the history
feat: ACI-2805 Add flag to follow xcode symlinks
  • Loading branch information
zsolt-marta-bitrise authored Sep 11, 2024
2 parents a5ed13e + c1dbce1 commit 9aa9b98
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 42 deletions.
24 changes: 24 additions & 0 deletions cmd/restoreXcodeDerivedDataFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ func restoreXcodeDerivedDataFilesCmdFn(ctx context.Context, authConfig common.Ca
return op, fmt.Errorf("restore DerivedData directories: %w", err)
}

if len(metadata.ProjectFiles.Symlinks) > 0 {
logger.TInfof("Restoring project symlinks")
if _, err = xcode.RestoreSymlinks(metadata.ProjectFiles.Symlinks, logger); err != nil {
return op, fmt.Errorf("restore project symlink: %w", err)
}
}

if len(metadata.DerivedData.Symlinks) > 0 {
logger.TInfof("Restoring DerivedData symlinks")
if _, err = xcode.RestoreSymlinks(metadata.DerivedData.Symlinks, logger); err != nil {
return op, fmt.Errorf("restore DerivedData symlink: %w", err)
}
}

if len(metadata.XcodeCacheDir.Files) > 0 {
logger.TInfof("Downloading Xcode cache files")
if _, err := xcode.DownloadCacheFilesFromBuildCache(ctx, metadata.XcodeCacheDir, kvClient, logger, isDebugLogMode, forceOverwrite, maxLoggedDownloadErrors); err != nil {
Expand All @@ -166,6 +180,13 @@ func restoreXcodeDerivedDataFilesCmdFn(ctx context.Context, authConfig common.Ca
}
}

if len(metadata.XcodeCacheDir.Symlinks) > 0 {
logger.TInfof("Restoring Xcode cache symlinks")
if _, err = xcode.RestoreSymlinks(metadata.XcodeCacheDir.Symlinks, logger); err != nil {
return op, fmt.Errorf("restore xcode symlink: %w", err)
}
}

return op, nil
}

Expand Down Expand Up @@ -234,8 +255,11 @@ func logCacheMetadata(md *xcode.Metadata, logger log.Logger, isDebugLogMode bool
logger.Infof(" Git commit: %s", md.GitCommit)
logger.Infof(" Git branch: %s", md.GitBranch)
logger.Infof(" Project files: %d", len(md.ProjectFiles.Files))
logger.Infof(" Project symlinks: %d", len(md.ProjectFiles.Symlinks))
logger.Infof(" DerivedData files: %d", len(md.DerivedData.Files))
logger.Infof(" DerivedData symlinks: %d", len(md.DerivedData.Symlinks))
logger.Infof(" Xcode cache files: %d", len(md.XcodeCacheDir.Files))
logger.Infof(" Xcode cache symlinks: %d", len(md.XcodeCacheDir.Symlinks))
logger.Infof(" Build Cache CLI version: %s", md.BuildCacheCLIVersion)
logger.Infof(" Metadata version: %d", md.MetadataVersion)

Expand Down
30 changes: 27 additions & 3 deletions cmd/saveXcodeDerivedDataFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var saveXcodeDerivedDataFilesCmd = &cobra.Command{
cacheKey, _ := cmd.Flags().GetString("key")
ddPath, _ := cmd.Flags().GetString("deriveddata-path")
xcodeCachePath, _ := cmd.Flags().GetString("xcodecache-path")
followSymlinks, _ := cmd.Flags().GetBool("follow-symlinks")

tracker := xcode.NewDefaultStepTracker("save-xcode-build-cache", os.Getenv, logger)
defer tracker.Wait()
Expand All @@ -49,7 +50,18 @@ var saveXcodeDerivedDataFilesCmd = &cobra.Command{
return fmt.Errorf("read auth config from environments: %w", err)
}

op, cmdError := saveXcodeDerivedDataFilesCmdFn(cmd.Context(), authConfig, CacheMetadataPath, projectRoot, cacheKey, ddPath, xcodeCachePath, logger, tracker, startT, os.Getenv)
op, cmdError := saveXcodeDerivedDataFilesCmdFn(cmd.Context(),
authConfig,
CacheMetadataPath,
projectRoot,
cacheKey,
ddPath,
xcodeCachePath,
followSymlinks,
logger,
tracker,
startT,
os.Getenv)
if op != nil {
if cmdError != nil {
errStr := cmdError.Error()
Expand Down Expand Up @@ -86,10 +98,21 @@ func init() {
panic(err)
}
saveXcodeDerivedDataFilesCmd.Flags().String("xcodecache-path", "", "Path to the Xcode cache directory folder to be saved. If not set, it will not be uploaded.")
saveXcodeDerivedDataFilesCmd.Flags().Bool("follow-symlinks", false, "Follow symlinks when calculating metadata and save referenced files to the cache (default: false)")
}

func saveXcodeDerivedDataFilesCmdFn(ctx context.Context, authConfig common.CacheAuthConfig, cacheMetadataPath, projectRoot, providedCacheKey, derivedDataPath, xcodeCachePath string,
logger log.Logger, tracker xcode.StepAnalyticsTracker, startT time.Time, envProvider func(string) string) (*xa.CacheOperation, error) {
func saveXcodeDerivedDataFilesCmdFn(ctx context.Context,
authConfig common.CacheAuthConfig,
cacheMetadataPath,
projectRoot,
providedCacheKey,
derivedDataPath,
xcodeCachePath string,
followSymlinks bool,
logger log.Logger,
tracker xcode.StepAnalyticsTracker,
startT time.Time,
envProvider func(string) string) (*xa.CacheOperation, error) {
var err error
var cacheKey string
if providedCacheKey == "" {
Expand Down Expand Up @@ -126,6 +149,7 @@ func saveXcodeDerivedDataFilesCmdFn(ctx context.Context, authConfig common.Cache
DerivedDataPath: derivedDataPath,
XcodeCacheDirPath: xcodeCachePath,
CacheKey: cacheKey,
FollowSymlinks: followSymlinks,
}, envProvider, logger)
if err != nil {
return op, fmt.Errorf("create metadata: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/saveXcodeDerivedDataFiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_saveXcodeDerivedDataFilesCmdFn(t *testing.T) {
envVars := createEnvProvider(map[string]string{
"BITRISEIO_BITRISE_SERVICES_ACCESS_TOKEN": "ServiceAccessTokenValue",
})
_, err := saveXcodeDerivedDataFilesCmdFn(context.Background(), common.CacheAuthConfig{}, "", "", "", "", "", mockLogger, mockTracker, time.Now(), envVars)
_, err := saveXcodeDerivedDataFilesCmdFn(context.Background(), common.CacheAuthConfig{}, "", "", "", "", "", false, mockLogger, mockTracker, time.Now(), envVars)

// then
require.EqualError(t, err, "get cache key: cache key is required if BITRISE_GIT_BRANCH env var is not set")
Expand Down
Loading

0 comments on commit 9aa9b98

Please sign in to comment.