Skip to content
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

feat(cmd/init): change default module name to project name #3303

Merged
merged 9 commits into from Mar 24, 2024

Conversation

Twacqwq
Copy link
Contributor

@Twacqwq Twacqwq commented Mar 11, 2024

Description

Change the default module name changeme to the project name.

$ wails init -n myproject -t vue-ts

Fixes #3277

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows
  • macOS
  • Linux

Test Configuration

Wails

Version | v2.8.0
Package Manager | pacman

System

- -
OS Arch Linux
Version TEMPLATE_VERSION_ID
ID arch
Go Version go1.21.5
Platform linux
Architecture amd64
CPU 13th Gen Intel(R) Core(TM) i7-13700
GPU Unknown
Memory 15GB

Dependencies

- -
Dependency Package Name
*docker docker
gcc gcc
libgtk-3 gtk3
libwebkit webkit2gtk
npm npm
pkg-config pkgconf

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • New Features
    • Introduced an option to automatically update the default module name to match the project name during project initialization.
    • Added functionality to update the module name in the go.mod file based on user input.
    • Added a new command line option -m to change the go.mod module name to the project name.
    • Changed the functionality to create a project with the project's name instead of the default name.

Copy link
Contributor

coderabbitai bot commented Mar 11, 2024

Walkthrough

The recent changes in the Wails project enhance the functionality to automatically set the Go module name in the go.mod file to match the project name. This improvement ensures a more intuitive and relevant default module name, replacing the generic placeholder. Changes include adding a new field in the Init struct and implementing functions to update the module name, along with a test for validation.

Changes

File Path Change Summary
v2/cmd/wails/init.go Added InitModule field to Init struct for changing default go.mod module name to project name.
v2/cmd/wails/internal/gomod/gomod.go Added ChangeModuleName function to update module name in go.mod file.
v2/internal/gomod/gomod.go Added UpdateGoModuleName function to update module name in a Go mod file.
v2/internal/gomod/gomod_test.go Added TestUpdateGoModName to test updating the Go module name in a Go mod file.
website/docs/reference/cli.mdx, website/src/pages/changelog.mdx Added options and flags to change go.mod module name to project name.

Assessment against linked issues

Objective Addressed Explanation
Propose Change: Default Module Name to Project Name (#3277)

Poem

Among the code where rabbits play,
Changes bloom in the light of day.
🌟 From "changeme" to names so clear,
Projects now sing, no placeholder fear.
🚀 With joy we code, our spirits light,
Wails project shines, in code so right.
A rabbit's touch, a magical spree,
🐰 Crafting dreams for you and me.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c5381dc and 504d951.
Files selected for processing (5)
  • v2/cmd/wails/flags/init.go (1 hunks)
  • v2/cmd/wails/init.go (2 hunks)
  • v2/cmd/wails/internal/gomod/gomod.go (1 hunks)
  • v2/internal/gomod/gomod.go (1 hunks)
  • v2/internal/gomod/gomod_test.go (1 hunks)
Additional comments: 4
v2/cmd/wails/flags/init.go (1)
  • 14-14: The addition of the InitModule field to the Init struct is correctly implemented, with appropriate tags for CLI parsing and a clear description. Ensure that the CLI documentation is updated to include the new -m parameter.
v2/internal/gomod/gomod.go (1)
  • 116-124: The UpdateGoModuleName function is implemented correctly, efficiently updating the module name in the go.mod file. It's worth considering future enhancements to handle edge cases, such as go.mod files without a module directive, though such cases are rare in practice.
v2/internal/gomod/gomod_test.go (1)
  • 155-180: The TestUpdateGoModName function correctly tests the functionality of updating the module name in the go.mod file. Consider adding more test cases to cover edge cases or unusual module names to ensure robustness.
v2/cmd/wails/init.go (1)
  • 129-135: The integration of changing the module name based on the InitModule flag within the initProject function is implemented correctly. Ensure this functionality is thoroughly tested across different environments to confirm its reliability.

Comment on lines 65 to 84
func ChangeModuleName(newModuleName string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
gomodFilename := fs.FindFileInParents(cwd, "go.mod")
if gomodFilename == "" {
return fmt.Errorf("no go.mod file found")
}
gomodData, err := os.ReadFile(gomodFilename)
if err != nil {
return err
}

gomodData, err = gomod.UpdateGoModuleName(gomodData, newModuleName)
if err != nil {
return err
}

return os.WriteFile(gomodFilename, gomodData, 0o755)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ChangeModuleName function is well-structured and follows a logical flow for updating the module name in the go.mod file. However, the file permissions for the go.mod file are set to 0o755 when writing back the changes. Typically, go.mod is a text file and should have permissions set to 0o644 to reflect that it's not an executable file.

- return os.WriteFile(gomodFilename, gomodData, 0o755)
+ return os.WriteFile(gomodFilename, gomodData, 0o644)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func ChangeModuleName(newModuleName string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
gomodFilename := fs.FindFileInParents(cwd, "go.mod")
if gomodFilename == "" {
return fmt.Errorf("no go.mod file found")
}
gomodData, err := os.ReadFile(gomodFilename)
if err != nil {
return err
}
gomodData, err = gomod.UpdateGoModuleName(gomodData, newModuleName)
if err != nil {
return err
}
return os.WriteFile(gomodFilename, gomodData, 0o755)
func ChangeModuleName(newModuleName string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
gomodFilename := fs.FindFileInParents(cwd, "go.mod")
if gomodFilename == "" {
return fmt.Errorf("no go.mod file found")
}
gomodData, err := os.ReadFile(gomodFilename)
if err != nil {
return err
}
gomodData, err = gomod.UpdateGoModuleName(gomodData, newModuleName)
if err != nil {
return err
}
return os.WriteFile(gomodFilename, gomodData, 0o644)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 504d951 and 8f7c65c.
Files selected for processing (1)
  • v2/cmd/wails/internal/gomod/gomod.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • v2/cmd/wails/internal/gomod/gomod.go

@@ -112,3 +112,13 @@ func SyncGoVersion(goModText []byte, goVersion string) ([]byte, bool, error) {

return goModText, true, nil
}

func UpdateGoModuleName(goModText []byte, newModuleName string) ([]byte, error) {
file, err := modfile.Parse("", goModText, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No doubt this works just fine, but I'm wondering if go mod edit -module <name> would be more forward compatible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! This is indeed a lack of consideration on my part. Compared with calling the golang.org/x/mod/modfile library to parse go.mod, the latter is more forward-compatible using commands. I will update its implementation and update the PR.

@leaanthony
Copy link
Member

This is a great start!
Please could you add an entry to the changelog located at website/src/pages/changelog.mdx?
We will also need to update the CLI reference documentation in website/docs/reference/cli.mdx.

@Twacqwq
Copy link
Contributor Author

Twacqwq commented Mar 11, 2024

This is a great start! Please could you add an entry to the changelog located at website/src/pages/changelog.mdx? We will also need to update the CLI reference documentation in website/docs/reference/cli.mdx.

of course I'd love to do that.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f7c65c and 34aff19.
Files selected for processing (3)
  • v2/cmd/wails/init.go (1 hunks)
  • website/docs/reference/cli.mdx (1 hunks)
  • website/src/pages/changelog.mdx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • v2/cmd/wails/init.go
Additional comments: 1
website/src/pages/changelog.mdx (1)
  • 30-30: The changelog entry for the new initModule command flag is clear, concise, and follows the established format. It correctly identifies the type of change, provides a brief description, and credits the contributor.

website/docs/reference/cli.mdx Outdated Show resolved Hide resolved
website/docs/reference/cli.mdx Outdated Show resolved Hide resolved
@Twacqwq Twacqwq requested a review from leaanthony March 11, 2024 15:44
@tmclane
Copy link
Member

tmclane commented Mar 13, 2024

I would also recommend that the default name of the module be set to the name of the directory created for the project if not overridden with the -m option.

i.e. use -n myproject as the default value since it is required.

@Twacqwq
Copy link
Contributor Author

Twacqwq commented Mar 14, 2024

I would also recommend that the default name of the module be set to the name of the directory created for the project if not overridden with the -m option.

i.e. use -n myproject as the default value since it is required.

Thanks for your suggestion, whenever I create a project, I manually modify go.mod, and it seems that the -m parameter is not necessary.

@Twacqwq
Copy link
Contributor Author

Twacqwq commented Mar 14, 2024

@leaanthony If I remove the -m parameter, is it okay to replace the module name of go.mod with the project name by default?

@leaanthony
Copy link
Member

Sure.

@leaanthony leaanthony added the awaiting feedback More information is required from the requestor label Mar 17, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 34aff19 and 93579cf.
Files selected for processing (2)
  • v2/cmd/wails/init.go (2 hunks)
  • website/src/pages/changelog.mdx (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • v2/cmd/wails/init.go
  • website/src/pages/changelog.mdx

@Twacqwq
Copy link
Contributor Author

Twacqwq commented Mar 17, 2024

@leaanthony Ready for review.

@leaanthony leaanthony merged commit bc69b39 into wailsapp:master Mar 24, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback More information is required from the requestor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Propose Change: Default Module Name to Project Name
3 participants