Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Sep 16, 2024
1 parent af88476 commit f1b4ccd
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 16 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# Espresso
*You wouldn't use a sledge hammer to hang a picture, would you?*
# [espresso](https://atc.kerosenelabs.io)
*You wouldn't use a sledgehammer to hang a picture frame, would you?*

Espresso is a Java build tool for modern development.

* **Simple:** Build for microservices and non-monoliths
* **Portable:** Built with Go, it can compile for every platform

## Features
TODO

## Getting Started
TODO
71 changes: 58 additions & 13 deletions internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,63 @@ import (
"github.com/spf13/cobra"
)

// GetProjectCommand returns the pre-built Cobra Command 'project'
func GetProjectCommand() *cobra.Command {
func GetCleanCommand() *cobra.Command {
var root = &cobra.Command{
Use: "project",
Short: "Manage a project within the current directory.",
Use: "clean",
Short: "Clean the build context",
Aliases: []string{"c"},
Run: func(cmd *cobra.Command, args []string) {
// get the config
cfg, err := GetConfig()
if err != nil {
fmt.Printf("An error occurred while reading the config: %s\n", err)
}

// get the build dir
buildPath, err := GetBuildPath(cfg)
if err != nil {
fmt.Printf("An error occurred while getting the build path: %s\n", err)
return
}

// get the dist dir
distPath, err := GetDistPath(cfg)
if err != nil {
fmt.Printf("An error occurred while getting the build path: %s\n", err)
return
}

// remove the build dir
err = os.RemoveAll(*buildPath)
if err != nil {
fmt.Printf("An error occurred while deleting the build path: %s\n", err)
return
}

// remove the dist dir
err = os.RemoveAll(*distPath)
if err != nil {
fmt.Printf("An error occurred while deleting the dist path: %s\n", err)
return
}
},
}
return root
}

var build = &cobra.Command{
// GetProjectCommand returns the pre-built Cobra Command 'project'
func GetBuildCommand() *cobra.Command {
var root = &cobra.Command{
Use: "build",
Short: "Build the project.",
Short: "Build the project, outputting a distributable.",
Aliases: []string{"b"},
Run: func(cmd *cobra.Command, args []string) {
// get the config
cfg, err := GetConfig()
if err != nil {
fmt.Printf("An error occurred while reading the config: %s\n", err)
}
fmt.Printf("Building '%s', please ensure you are compliant with all dependency licenses\n", cfg.Name)

// discover source files
files, err := DiscoverSourceFiles(cfg)
Expand All @@ -46,12 +86,19 @@ func GetProjectCommand() *cobra.Command {
wg.Wait()

// package the project
println("Packaging")
err = PackageClasses(cfg)
if err != nil {
fmt.Printf("An error occurred while packaging the classes: %s\n", err)
}
println("Done")
},
}
root.AddCommand(build)
return root
}

var init = &cobra.Command{
func GetInitCommand() *cobra.Command {
var root = &cobra.Command{
Use: "init",
Short: "Initialize a new project.",
Aliases: []string{"i"},
Expand Down Expand Up @@ -106,11 +153,9 @@ func GetProjectCommand() *cobra.Command {
println("Done.")
},
}
init.Flags().StringP("name", "n", "", "Name of the project")
init.Flags().StringP("package", "p", "org.example.myapp", "Base package of the application")
init.MarkFlagRequired("name")
root.AddCommand(init)

root.Flags().StringP("name", "n", "", "Name of the project")
root.Flags().StringP("package", "p", "org.example.myapp", "Base package of the application")
root.MarkFlagRequired("name")
return root
}

Expand Down
79 changes: 79 additions & 0 deletions internal/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package internal

import (
"fmt"
"os"
"os/exec"
)

// GenerateManifest generates a JVM manifest
func GenerateManifest(cfg *ProjectConfig) string {
base := "Manifest-Version: 1.0\n"
base += "Main-Class: " + cfg.BasePackage + ".Main\n"
base += "Created-By: Espresso"
return base
}

// Write the Manifest to the build directory
func WriteManifest(cfg *ProjectConfig) error {
// get the path where it should live
buildPath, err := GetBuildPath(cfg)
path := *buildPath + "/MANIFEST.MF"
if err != nil {
return err
}

// open the file
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()

// write the file
content := GenerateManifest(cfg)
_, err = file.WriteString(content)
if err != nil {
return err
}
return nil
}

// PackageClasses creates a .jar of the given classes
func PackageClasses(cfg *ProjectConfig) error {
command := cfg.Toolchain.Path + "/bin/jar"
args := []string{"cfm"}

// handle jar output path
if IsDebugMode() {
args = append(args, "ESPRESSO_DEBUG/dist/dist.jar")
} else {
args = append(args, "dist/dist.jar")
}

// write the manifest, include it
WriteManifest(cfg)
if IsDebugMode() {
args = append(args, "ESPRESSO_DEBUG/build/MANIFEST.MF")
} else {
args = append(args, "build/MANIFEST.MF")
}

// add the class directory
if IsDebugMode() {
args = append(args, "-C", "ESPRESSO_DEBUG/build")
} else {
args = append(args, "-C", "build")
}
args = append(args, ".")
// run the command
fmt.Printf("Running: %s %s\n", command, args)
cmd := exec.Command(command, args...)
output, err := cmd.Output()
if err != nil {
println(string(output))
return err
}

return nil
}
16 changes: 16 additions & 0 deletions internal/fs.go → internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,19 @@ func GetBuildPath(cfg *ProjectConfig) (*string, error) {
path += "/build"
return &path, nil
}

// GetDistPath gets the absolute path to the dist directory
func GetDistPath(cfg *ProjectConfig) (*string, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
path := wd

if IsDebugMode() {
path += "/ESPRESSO_DEBUG"
}

path += "/dist"
return &path, nil
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ func main() {
}

// project commands
root.AddCommand(internal.GetProjectCommand())
root.AddCommand(internal.GetCleanCommand())
root.AddCommand(internal.GetBuildCommand())
root.AddCommand(internal.GetInitCommand())
root.AddCommand(internal.GetDependencyCommand())

// execute
Expand Down

0 comments on commit f1b4ccd

Please sign in to comment.