From ec593176fcc120604c5d627446ad562ca4541adc Mon Sep 17 00:00:00 2001 From: Hunter LaFaille Date: Mon, 16 Sep 2024 18:55:00 +0000 Subject: [PATCH] refactoring --- dependency/command.go | 41 --------------- dependency/resolver.go | 1 - internal/build.go | 26 ++++++++++ {project => internal}/command.go | 51 ++++++++++++++++++- project/handler.go => internal/config.go | 2 +- internal/context.go | 1 + {project => internal}/fs.go | 2 +- .../source_discovery.go | 2 +- main.go | 7 ++- project/build.go | 1 - 10 files changed, 82 insertions(+), 52 deletions(-) delete mode 100644 dependency/command.go delete mode 100644 dependency/resolver.go create mode 100644 internal/build.go rename {project => internal}/command.go (65%) rename project/handler.go => internal/config.go (99%) create mode 100644 internal/context.go rename {project => internal}/fs.go (98%) rename project/discover.go => internal/source_discovery.go (98%) delete mode 100644 project/build.go diff --git a/dependency/command.go b/dependency/command.go deleted file mode 100644 index 0f85ebe..0000000 --- a/dependency/command.go +++ /dev/null @@ -1,41 +0,0 @@ -package dependency - -import "github.com/spf13/cobra" - -func AssembleCommandHierarchy() *cobra.Command { - var root = &cobra.Command{ - Use: "dependency", - Short: "Manage dependencies for the project within the current directory.", - } - - var query = &cobra.Command{ - Use: "query", - Short: "Query the registries for the given search term.", - Aliases: []string{"q"}, - Run: func(cmd *cobra.Command, args []string) { - println("TODO") - }, - } - query.Flags().StringP("term", "t", "", "Term to query by") - query.MarkFlagRequired("term") - root.AddCommand(query) - - var sync = &cobra.Command{ - Use: "sync", - Short: "Sync dependencies declared in the project configuration with dependencies on the local filesystem.", - Run: func(cmd *cobra.Command, args []string) { - println("TODO") - }, - } - root.AddCommand(sync) - - var add = &cobra.Command{ - Use: "add", - Short: "Add a dependency to the project configuration.", - Run: func(cmd *cobra.Command, args []string) { - println("TODO") - }, - } - root.AddCommand(add) - return root -} diff --git a/dependency/resolver.go b/dependency/resolver.go deleted file mode 100644 index 9f32568..0000000 --- a/dependency/resolver.go +++ /dev/null @@ -1 +0,0 @@ -package dependency diff --git a/internal/build.go b/internal/build.go new file mode 100644 index 0000000..d24a919 --- /dev/null +++ b/internal/build.go @@ -0,0 +1,26 @@ +package internal + +import ( + "os/exec" +) + +// CompileSourceFile compiles the sourcefile with the given project toolchain +func CompileSourceFile(cfg *ProjectConfig, srcFile *SourceFile) error { + // run the compiler + command := cfg.Toolchain.Path + "/bin/javac" + args := []string{} + if IsDebugMode() { + args = append(args, "-d", "ESPRESSO_DEBUG/build") + } else { + args = append(args, "build") + } + args = append(args, srcFile.Path) + cmd := exec.Command(command, args...) + + // handle output + _, err := cmd.Output() + if err != nil { + return err + } + return nil +} diff --git a/project/command.go b/internal/command.go similarity index 65% rename from project/command.go rename to internal/command.go index 8fd8fef..6671f1e 100644 --- a/project/command.go +++ b/internal/command.go @@ -1,4 +1,4 @@ -package project +package internal import ( "fmt" @@ -8,7 +8,7 @@ import ( "hlafaille.xyz/espresso/v0/toolchain" ) -func AssembleCommandHierarchy() *cobra.Command { +func GetProjectCommand() *cobra.Command { var root = &cobra.Command{ Use: "project", Short: "Manage a project within the current directory.", @@ -31,6 +31,15 @@ func AssembleCommandHierarchy() *cobra.Command { fmt.Printf("An error occurred while discovering source files: %s\n", err) } fmt.Printf("Discovered %d source file(s)\n", len(files)) + + // run the compiler on each source file + for _, value := range files { + println("Compiling: " + value.Path) + CompileSourceFile(cfg, &value) + } + + // package the project + println("Done") }, } root.AddCommand(build) @@ -97,3 +106,41 @@ func AssembleCommandHierarchy() *cobra.Command { return root } + +func GetDependencyCommand() *cobra.Command { + var root = &cobra.Command{ + Use: "dependency", + Short: "Manage dependencies for the project within the current directory.", + } + + var query = &cobra.Command{ + Use: "query", + Short: "Query the registries for the given search term.", + Aliases: []string{"q"}, + Run: func(cmd *cobra.Command, args []string) { + println("TODO") + }, + } + query.Flags().StringP("term", "t", "", "Term to query by") + query.MarkFlagRequired("term") + root.AddCommand(query) + + var sync = &cobra.Command{ + Use: "sync", + Short: "Sync dependencies declared in the project configuration with dependencies on the local filesystem.", + Run: func(cmd *cobra.Command, args []string) { + println("TODO") + }, + } + root.AddCommand(sync) + + var add = &cobra.Command{ + Use: "add", + Short: "Add a dependency to the project configuration.", + Run: func(cmd *cobra.Command, args []string) { + println("TODO") + }, + } + root.AddCommand(add) + return root +} diff --git a/project/handler.go b/internal/config.go similarity index 99% rename from project/handler.go rename to internal/config.go index c1b6a31..729411e 100644 --- a/project/handler.go +++ b/internal/config.go @@ -1,4 +1,4 @@ -package project +package internal import ( "os" diff --git a/internal/context.go b/internal/context.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/context.go @@ -0,0 +1 @@ +package internal diff --git a/project/fs.go b/internal/fs.go similarity index 98% rename from project/fs.go rename to internal/fs.go index 4c0969c..9822ad7 100644 --- a/project/fs.go +++ b/internal/fs.go @@ -1,4 +1,4 @@ -package project +package internal import ( "os" diff --git a/project/discover.go b/internal/source_discovery.go similarity index 98% rename from project/discover.go rename to internal/source_discovery.go index 72842ce..459b892 100644 --- a/project/discover.go +++ b/internal/source_discovery.go @@ -1,4 +1,4 @@ -package project +package internal import ( "io/fs" diff --git a/main.go b/main.go index ef38b89..4643da7 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,7 @@ package main import ( "github.com/spf13/cobra" - "hlafaille.xyz/espresso/v0/dependency" - "hlafaille.xyz/espresso/v0/project" + "hlafaille.xyz/espresso/v0/internal" ) func main() { @@ -13,8 +12,8 @@ func main() { } // project commands - root.AddCommand(project.AssembleCommandHierarchy()) - root.AddCommand(dependency.AssembleCommandHierarchy()) + root.AddCommand(internal.GetProjectCommand()) + root.AddCommand(internal.GetDependencyCommand()) // execute root.Execute() diff --git a/project/build.go b/project/build.go deleted file mode 100644 index 7dac6f1..0000000 --- a/project/build.go +++ /dev/null @@ -1 +0,0 @@ -package project