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

#105 Generate different code if users chooses single file option #142

19 changes: 7 additions & 12 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import (
)

var (
buildDestination string
buildCustomName string
buildOneFile bool
buildModelsSymlink bool
buildLibrary string
buildController = controller.BuildController{}
)

// buildCmd represents the build command
Expand All @@ -23,20 +19,19 @@ var buildCmd = &cobra.Command{
Note: if you want to use nuitka, you need to have a working C compiler.`,
Run: runBuild,
}
var buildController = controller.BuildController{}

func runBuild(cmd *cobra.Command, args []string) {
err := buildController.Run(buildCustomName, buildLibrary, buildDestination, buildOneFile, buildModelsSymlink)
err := buildController.Run()
if err != nil {
app.UI().Error().Println(err.Error())
os.Exit(1)
}
}

func init() {
buildCmd.Flags().StringVarP(&buildDestination, "out-dir", "o", "dist", "Destination directory where the project will be built")
buildCmd.Flags().StringVarP(&buildCustomName, "name", "n", "", "Custom name for the executable")
buildCmd.Flags().StringVarP(&buildLibrary, "library", "l", "pyinstaller", "Library to use for building the project (select between pyinstaller and nuitka)")
buildCmd.Flags().BoolVarP(&buildOneFile, "one-file", "f", false, "Build the project in one file")
buildCmd.Flags().BoolVarP(&buildModelsSymlink, "models-symlink", "s", false, "Symlink the models directory to the build directory")
buildCmd.Flags().StringVarP(&buildController.DestinationDir, "out-dir", "o", "dist", "DestinationDir directory where the project will be built")
buildCmd.Flags().StringVarP(&buildController.CustomName, "name", "n", "", "Custom name for the executable")
buildCmd.Flags().StringVarP(&buildController.Library, "library", "l", "pyinstaller", "Library to use for building the project (select between pyinstaller and nuitka)")
buildCmd.Flags().BoolVarP(&buildController.OneFile, "one-file", "f", false, "Build the project in one file")
buildCmd.Flags().BoolVarP(&buildController.ModelsSymlink, "models-symlink", "s", false, "Symlink the models directory to the build directory")
}
14 changes: 10 additions & 4 deletions cmd/model/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/easy-model-fusion/emf-cli/internal/downloader/model"
"github.com/easy-model-fusion/emf-cli/pkg/huggingface"
"github.com/spf13/cobra"
"os"
)

// addCmd represents the add model by names command
Expand All @@ -17,8 +18,8 @@ var modelAddCmd = &cobra.Command{
}

var (
customArgs downloadermodel.Args
authorizeDownload bool
customArgs downloadermodel.Args
addController = modelcontroller.AddController{}
)

func init() {
Expand All @@ -28,10 +29,15 @@ func init() {
// Bind cobra args to the downloader script args
customArgs.ToCobra(modelAddCmd)
customArgs.DirectoryPath = app.DownloadDirectoryPath
modelAddCmd.Flags().BoolVarP(&authorizeDownload, "yes", "y", false, "Automatic yes to prompts")
modelAddCmd.Flags().BoolVarP(&addController.AuthorizeDownload, "yes", "y", false, "Automatic yes to prompts")
modelAddCmd.Flags().BoolVarP(&addController.SingleFile, "single-file", "S", false, "Use the model as a single file, (usually its a safetensors file)")
}

// runAddByNames runs the add command to add models by name
func runAdd(cmd *cobra.Command, args []string) {
modelcontroller.RunAdd(args, customArgs, authorizeDownload)
err := addController.Run(args, customArgs)
if err != nil {
app.UI().Error().Println(err.Error())
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion internal/codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (cg *PythonCodeGenerator) VisitImport(importStmt *Import) error {
// VisitImportWhat visits an ImportWhat node
func (cg *PythonCodeGenerator) VisitImportWhat(importWhat *ImportWhat) error {
if importWhat.Name == "" {
return errors.New("import what name cannot be empty")
return errors.New("import what \"name\" cannot be empty")
}

cg.append(importWhat.Name)
Expand Down
62 changes: 34 additions & 28 deletions internal/controller/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,44 @@ import (
"time"
)

type BuildController struct{}
type BuildController struct {
DestinationDir string
CustomName string
OneFile bool
ModelsSymlink bool
Library string
}

// Run runs the build command
func (bc BuildController) Run(customName, library, destDir string, oneFile, modelsSymlink bool) error {
func (bc BuildController) Run() error {
if err := config.GetViperConfig("."); err != nil {
return err
}

sdk.SendUpdateSuggestion()

if library != "pyinstaller" && library != "nuitka" {
if bc.Library != "pyinstaller" && bc.Library != "nuitka" {
return fmt.Errorf("invalid library selected")
}

// check if destDir exists
if _, err := os.Stat(destDir); os.IsNotExist(err) {
app.UI().Info().Println(fmt.Sprintf("Creating dist folder %s", destDir))
err = os.Mkdir(destDir, os.ModePerm)
if _, err := os.Stat(bc.DestinationDir); os.IsNotExist(err) {
app.UI().Info().Println(fmt.Sprintf("Creating dist folder %s", bc.DestinationDir))
err = os.Mkdir(bc.DestinationDir, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating dist folder: %s", err.Error())
}
}

// Install dependencies
pythonPath, err := bc.InstallDependencies(library)
pythonPath, err := bc.InstallDependencies(bc.Library)
if err != nil {
return err
}

var libraryPath string

switch library {
switch bc.Library {
case "pyinstaller":
libraryPath, err = app.Python().FindVEnvExecutable(".venv", "pyinstaller")
if err != nil {
Expand All @@ -65,49 +71,49 @@ func (bc BuildController) Run(customName, library, destDir string, oneFile, mode
}

// Build the project
err = bc.Build(customName, library, destDir, libraryPath, oneFile)
err = bc.Build(libraryPath)
if err != nil {
return err
}

if !modelsSymlink {
if !bc.ModelsSymlink {
return nil
}

// Create symbolic link to models
err = bc.createModelsSymbolicLink(destDir)
err = bc.createModelsSymbolicLink()
if err != nil {
return fmt.Errorf("error creating symbolic link: %s", err.Error())
}
return nil
}

// createBuildArgs creates the arguments for the build command
func (bc BuildController) createBuildArgs(customName, library, destDir string, oneFile bool) []string {
func (bc BuildController) createBuildArgs() []string {
var buildArgs []string

if customName == "" {
customName = viper.GetString("name")
if bc.CustomName == "" {
bc.CustomName = viper.GetString("name")
}

switch library {
switch bc.Library {
case "pyinstaller":
if oneFile {
if bc.OneFile {
buildArgs = append(buildArgs, "-F")
}

buildArgs = append(buildArgs, fmt.Sprintf("--name=%s", customName))
buildArgs = append(buildArgs, fmt.Sprintf("--distpath=%s", destDir))
buildArgs = append(buildArgs, fmt.Sprintf("--name=%s", bc.CustomName))
buildArgs = append(buildArgs, fmt.Sprintf("--distpath=%s", bc.DestinationDir))
buildArgs = append(buildArgs, viper.GetStringSlice("build.pyinstaller.args")...)
case "nuitka":
buildArgs = append(buildArgs, "-m nuitka")

if oneFile {
if bc.OneFile {
buildArgs = append(buildArgs, "--onefile")
}

buildArgs = append(buildArgs, fmt.Sprintf("--python-flag=-o %s", customName))
buildArgs = append(buildArgs, fmt.Sprintf("--output-dir=%s", destDir))
buildArgs = append(buildArgs, fmt.Sprintf("--python-flag=-o %s", bc.CustomName))
buildArgs = append(buildArgs, fmt.Sprintf("--output-dir=%s", bc.DestinationDir))
buildArgs = append(buildArgs, viper.GetStringSlice("build.nuitka.args")...)
}

Expand Down Expand Up @@ -138,12 +144,12 @@ func (bc BuildController) InstallDependencies(library string) (string, error) {
}

// Build builds the project
func (bc BuildController) Build(customName, library, destDir, libraryPath string, oneFile bool) (err error) {
buildArgs := bc.createBuildArgs(customName, library, destDir, oneFile)
func (bc BuildController) Build(libraryPath string) (err error) {
buildArgs := bc.createBuildArgs()

app.UI().Info().Println(fmt.Sprintf("Building project using %s...", library))
app.UI().Info().Println(fmt.Sprintf("Building project using %s...", bc.Library))
app.UI().Info().Println(fmt.Sprintf("Using the following arguments: %s", buildArgs))
app.UI().Info().Println(fmt.Sprintf("The project will be built to %s", destDir))
app.UI().Info().Println(fmt.Sprintf("The project will be built to %s", bc.DestinationDir))

// Setup signal catching
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -184,10 +190,10 @@ func (bc BuildController) Build(customName, library, destDir, libraryPath string
}

// createModelsSymbolicLink creates a symbolic link to the models folder
func (bc BuildController) createModelsSymbolicLink(destDir string) error {
func (bc BuildController) createModelsSymbolicLink() error {
// Create symbolic link to models
modelsPath := "models"
distPath := path.Join(destDir, "models")
distPath := path.Join(bc.DestinationDir, "models")

app.UI().Info().Println(fmt.Sprintf("Creating symbolic link from %s to %s", modelsPath, distPath))

Expand All @@ -197,7 +203,7 @@ func (bc BuildController) createModelsSymbolicLink(destDir string) error {
}

// Check if dist folder exists
if _, err := os.Stat(destDir); os.IsNotExist(err) {
if _, err := os.Stat(bc.DestinationDir); os.IsNotExist(err) {
return fmt.Errorf("dist folder does not exist")
}

Expand Down
Loading
Loading