amtoine / nu-git-manager

A collection of Nushell tools to manage Git repositories.
GNU General Public License v3.0
26 stars 2 forks source link

Refactor command naming convention into actual modules #184

Closed texastoland closed 5 months ago

texastoland commented 5 months ago

This PR enables the following:

use nu-git-manager-sugar gm # all gm commands
use gm gh # shortcut for gh commands

Mostly mechanical changes. Module contents were left unindented to improve diff readability. 1 innocuous change took me hours and needs documented for future maintenance:

--- a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/mod.nu
+++ b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/mod.nu
 export module git/
 export module github.nu
 export module dotfiles.nu
+# workarounds below explained in #184
+export module gm {
+    use extra.nu gm; export use gm *
+    export module repo {
+        use git/ gm repo; export use repo *
+    }
+    export module gh {
+        use github.nu gm gh; export use gh *
+    }
+    export module cfg {
+        use dotfiles.nu gm cfg; export use cfg *
+    }
}

[!WARNING]

Here be dragons šŸ²

My first attempt was to export use each module:

export use extra.nu
export use extra gm
export use git/
export use git gm
# etc

But export use partially hides nested modules (nushell/nushell#12066) so:

export module extra.nu # revert to export module
export use extra.nu gm # export use file path
export module git/
export use git/ gm
# etc

But export use shadows modules instead of merging them (TODO: report bug) so:

export module extra.nu
export module git/
# etc
export module gm { # wrapping module
    export use extra.nu gm * # all child members
    export use git/ gm *
    # etc
}

But export use doesn't permit multiple module chain members (nushell/nushell#12057) so:

export module extra.nu
export module git/
# etc
export module gm {
    use extra.nu gm; export use gm * # use before export use
    use git/ gm; export use gm *
    # etc
}

But export use still hides submodules so:

export module extra.nu
export module git/
# etc
export module gm {
    use extra.nu gm; export use gm *
    export module repo { # another wrapping module
        use git/ gm repo; export use repo * # all grandchildren
    }
    # etc
}

None of these workarounds would be necessary using 1 file per submodule because they could be passed to export module above instead of export use. A less brittle alternative would be to create a directory for each category module, 1 script for each leaf module, and 1 mod.nu to wrap it in the gm namespace. For example:

šŸ“‚ github
  šŸ“„ gh.nu
    šŸ„” everthing in github.nu now
  šŸ“„ mod.nu
    šŸ“¦ export module gm { export module gh.nu }
    šŸ„” anything outside gh goes here
texastoland commented 5 months ago

Closing in favor of #185 for now. I attempted this way because it's more minimal than the directory approach. But 3 specific bugs in export use make it brittle and leave an unresolved issue re-exporting main commands (see FIXME above).