-
Notifications
You must be signed in to change notification settings - Fork 19
Automate swift package resolve
#2162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,9 @@ func postProcessCreatedPlugins(ctx context.Context, plugins []createdPlugin) err | |
| if err := recreateNPMPackageLock(ctx, plugin); err != nil { | ||
| return fmt.Errorf("failed to recreate package-lock.json for %s: %w", newPluginRef, err) | ||
| } | ||
| if err := recreateSwiftPackageResolved(ctx, plugin); err != nil { | ||
| return fmt.Errorf("failed to resolve Swift package for %s: %w", newPluginRef, err) | ||
| } | ||
| } | ||
| if err := runPluginTests(ctx, plugins); err != nil { | ||
| return fmt.Errorf("failed to run plugin tests: %w", err) | ||
|
|
@@ -119,6 +122,97 @@ func recreateNPMPackageLock(ctx context.Context, plugin createdPlugin) error { | |
| return cmd.Run() | ||
| } | ||
|
|
||
| // recreateSwiftPackageResolved resolves Swift package dependencies for plugins that use Swift packages. | ||
| // It clones the git repository specified in the Dockerfile, runs 'swift package resolve', | ||
| // and moves the generated Package.resolved file to the version directory. | ||
| func recreateSwiftPackageResolved(ctx context.Context, plugin createdPlugin) error { | ||
| versionDir := filepath.Join(plugin.pluginDir, plugin.newVersion) | ||
| packageResolved := filepath.Join(versionDir, "Package.resolved") | ||
| _, err := os.Stat(packageResolved) | ||
| if err != nil { | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return err | ||
| } | ||
| // no Package.resolved to update | ||
| return nil | ||
| } | ||
|
|
||
| // Read the Dockerfile to find the git clone command | ||
| dockerfile := filepath.Join(versionDir, "Dockerfile") | ||
| file, err := os.Open(dockerfile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open Dockerfile: %w", err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| var gitCloneCmd string | ||
| scanner := bufio.NewScanner(file) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if strings.Contains(line, "RUN git clone") { | ||
| // Strip the "RUN " prefix | ||
| gitCloneCmd = strings.TrimSpace(strings.TrimPrefix(line, "RUN ")) | ||
| break | ||
| } | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return fmt.Errorf("failed to read Dockerfile: %w", err) | ||
| } | ||
| if gitCloneCmd == "" { | ||
| return errors.New("no 'RUN git clone' command found in Dockerfile") | ||
| } | ||
|
|
||
| log.Printf("resolving Swift package for %s/%s:%s", plugin.org, plugin.name, plugin.newVersion) | ||
|
|
||
| // Execute the git clone command | ||
| cmd := exec.CommandContext(ctx, "sh", "-c", gitCloneCmd) | ||
| cmd.Dir = versionDir | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| if err := cmd.Run(); err != nil { | ||
| return fmt.Errorf("failed to run git clone: %w", err) | ||
| } | ||
|
|
||
| // Extract the repository name from the git clone command to determine the directory | ||
| parts := strings.Fields(gitCloneCmd) | ||
| var repoDir string | ||
| for _, part := range parts { | ||
| if strings.HasPrefix(part, "https://") { | ||
|
||
| // Extract directory name from URL (e.g., "repo.git" or "repo") | ||
| repoName := filepath.Base(part) | ||
| repoName = strings.TrimSuffix(repoName, ".git") | ||
| repoDir = filepath.Join(versionDir, repoName) | ||
| break | ||
| } | ||
| } | ||
| if repoDir == "" { | ||
| return errors.New("failed to determine repository directory from git clone command") | ||
| } | ||
|
|
||
| // Run `swift package resolve` in the cloned directory | ||
| cmd = exec.CommandContext(ctx, "swift", "package", "resolve") | ||
stefanvanburen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cmd.Dir = repoDir | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| if err := cmd.Run(); err != nil { | ||
| return fmt.Errorf("failed to run swift package resolve: %w", err) | ||
| } | ||
|
|
||
| // Move the Package.resolved file from the cloned directory to the version directory | ||
| src := filepath.Join(repoDir, "Package.resolved") | ||
| dest := packageResolved | ||
| if err := os.Rename(src, dest); err != nil { | ||
| return fmt.Errorf("failed to move Package.resolved: %w", err) | ||
| } | ||
|
|
||
| // Remove the cloned repo | ||
| if err := os.RemoveAll(repoDir); err != nil { | ||
| return fmt.Errorf("removing cloned repo: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // runPluginTests runs 'make test PLUGINS="org/name:v<new>"' in order to generate plugin.sum files. | ||
| func runPluginTests(ctx context.Context, plugins []createdPlugin) error { | ||
| pluginsEnv := make([]string, 0, len(plugins)) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.