Skip to content

Move packaging to containers and add windows-msi and linux-appimage #30

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 81 additions & 78 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"runtime"
"strings"

"github.com/go-flutter-desktop/hover/internal/enginecache"
"github.com/go-flutter-desktop/hover/internal/log"
"github.com/go-flutter-desktop/hover/internal/versioncheck"
"github.com/hashicorp/go-version"
"github.com/otiai10/copy"
"github.com/spf13/cobra"

"github.com/go-flutter-desktop/hover/internal/enginecache"
"github.com/go-flutter-desktop/hover/internal/log"
"github.com/go-flutter-desktop/hover/internal/versioncheck"
"github.com/go-flutter-desktop/hover/internal/build"
"github.com/go-flutter-desktop/hover/internal/pubspec"
"github.com/go-flutter-desktop/hover/cmd/packaging"
)

var dotSlash = string([]byte{'.', filepath.Separator})
Expand All @@ -31,8 +35,6 @@ var (
buildDocker bool
)

const buildPath = "go"

const mingwGccBinName = "x86_64-w64-mingw32-gcc"
const clangBinName = "o32-clang"

Expand All @@ -49,8 +51,10 @@ func init() {
buildCmd.AddCommand(buildLinuxCmd)
buildCmd.AddCommand(buildLinuxSnapCmd)
buildCmd.AddCommand(buildLinuxDebCmd)
buildCmd.AddCommand(buildLinuxAppImageCmd)
buildCmd.AddCommand(buildDarwinCmd)
buildCmd.AddCommand(buildWindowsCmd)
buildCmd.AddCommand(buildWindowsMsiCmd)
rootCmd.AddCommand(buildCmd)
}

Expand All @@ -63,100 +67,98 @@ var buildLinuxCmd = &cobra.Command{
Use: "linux",
Short: "Build a desktop release for linux",
Run: func(cmd *cobra.Command, args []string) {
projectName := getPubSpec().Name
assertHoverInitialized()

build(projectName, "linux", nil)
buildNormal("linux", nil)
},
}

var buildLinuxSnapCmd = &cobra.Command{
Use: "linux-snap",
Short: "Build a desktop release for linux and package it for snap",
Run: func(cmd *cobra.Command, args []string) {
projectName := getPubSpec().Name
assertHoverInitialized()
assertPackagingFormatInitialized("linux-snap")
packaging.AssertPackagingFormatInitialized("linux-snap")

if !packaging.DockerInstalled() {
os.Exit(1)
}

build(projectName, "linux", nil)
buildLinuxSnap(projectName)
buildNormal("linux", nil)
packaging.BuildLinuxSnap()
},
}

var buildLinuxDebCmd = &cobra.Command{
Use: "linux-deb",
Short: "Build a desktop release for linux and package it for deb",
Run: func(cmd *cobra.Command, args []string) {
projectName := getPubSpec().Name
assertHoverInitialized()
assertPackagingFormatInitialized("linux-deb")
packaging.AssertPackagingFormatInitialized("linux-deb")

if !packaging.DockerInstalled() {
os.Exit(1)
}

buildNormal("linux", nil)
packaging.BuildLinuxDeb()
},
}

var buildLinuxAppImageCmd = &cobra.Command{
Use: "linux-appimage",
Short: "Build a desktop release for linux and package it for AppImage",
Run: func(cmd *cobra.Command, args []string) {
assertHoverInitialized()
packaging.AssertPackagingFormatInitialized("linux-appimage")

if !packaging.DockerInstalled() {
os.Exit(1)
}

build(projectName, "linux", nil)
buildLinuxDeb(projectName)
buildNormal("linux", nil)
packaging.BuildLinuxAppImage()
},
}

var buildDarwinCmd = &cobra.Command{
Use: "darwin",
Short: "Build a desktop release for darwin",
Run: func(cmd *cobra.Command, args []string) {
projectName := getPubSpec().Name
assertHoverInitialized()

build(projectName, "darwin", nil)
buildNormal("darwin", nil)
},
}

var buildWindowsCmd = &cobra.Command{
Use: "windows",
Short: "Build a desktop release for windows",
Run: func(cmd *cobra.Command, args []string) {
projectName := getPubSpec().Name
assertHoverInitialized()

build(projectName, "windows", nil)
buildNormal("windows", nil)
},
}

func outputDirectoryPath(targetOS string) string {
outputDirectoryPath, err := filepath.Abs(filepath.Join(buildPath, "build", "outputs", targetOS))
if err != nil {
log.Errorf("Failed to resolve absolute path for output directory: %v", err)
os.Exit(1)
}
if _, err := os.Stat(outputDirectoryPath); os.IsNotExist(err) {
err = os.MkdirAll(outputDirectoryPath, 0775)
if err != nil {
log.Errorf("Failed to create output directory %s: %v", outputDirectoryPath, err)
var buildWindowsMsiCmd = &cobra.Command{
Use: "windows-msi",
Short: "Build a desktop release for windows and package it for msi",
Run: func(cmd *cobra.Command, args []string) {
assertHoverInitialized()
packaging.AssertPackagingFormatInitialized("windows-msi")

if !packaging.DockerInstalled() {
os.Exit(1)
}
}
return outputDirectoryPath
}

func outputBinaryName(projectName string, targetOS string) string {
var outputBinaryName = projectName
switch targetOS {
case "darwin":
// no special filename
case "linux":
// no special filename
case "windows":
outputBinaryName += ".exe"
default:
log.Errorf("Target platform %s is not supported.", targetOS)
os.Exit(1)
}
return outputBinaryName
}

func outputBinaryPath(projectName string, targetOS string) string {
outputBinaryPath := filepath.Join(outputDirectoryPath(targetOS), outputBinaryName(projectName, targetOS))
return outputBinaryPath
buildNormal("windows", nil)
packaging.BuildWindowsMsi()
},
}

func dockerBuild(projectName string, targetOS string, vmArguments []string) {
crossCompilingDir, err := filepath.Abs(filepath.Join(buildPath, "cross-compiling"))
func buildInDocker(targetOS string, vmArguments []string) {
crossCompilingDir, err := filepath.Abs(filepath.Join(build.BuildPath, "cross-compiling"))
err = os.MkdirAll(crossCompilingDir, 0755)
if err != nil {
log.Errorf("Cannot create the cross-compiling directory: %v", err)
Expand Down Expand Up @@ -205,9 +207,10 @@ func dockerBuild(projectName string, targetOS string, vmArguments []string) {
log.Errorf("Could not close Dockerfile: %v", err)
os.Exit(1)
}
log.Infof("A Dockerfile for cross-compiling for %s has been created at %s. You can add it to git.", targetOS, filepath.Join(buildPath, "cross-compiling", targetOS))
log.Infof("A Dockerfile for cross-compiling for %s has been created at %s. You can add it to git.", targetOS, filepath.Join(build.BuildPath, "cross-compiling", targetOS))
}
dockerBuildCmd := exec.Command(dockerBin, "build", "-t", "hover-build-cc", ".")
dockerBuildCmd := exec.Command(build.DockerBin, "build", "-t", "hover-build-cc", ".")
dockerBuildCmd.Stdout = os.Stdout
dockerBuildCmd.Stderr = os.Stderr
dockerBuildCmd.Dir = crossCompilingDir
err = dockerBuildCmd.Run()
Expand Down Expand Up @@ -239,8 +242,8 @@ func dockerBuild(projectName string, targetOS string, vmArguments []string) {
if runtime.GOOS != "windows" {
chownStr = fmt.Sprintf(" && chown %s:%s build/ -R", u.Uid, u.Gid)
}
args = append(args, "bash", "-c", fmt.Sprintf("%s%s", strings.Join(buildCommand(targetOS, vmArguments, "build/outputs/"+targetOS+"/"+outputBinaryName(projectName, targetOS)), " "), chownStr))
dockerRunCmd := exec.Command(dockerBin, args...)
args = append(args, "bash", "-c", fmt.Sprintf("%s%s", strings.Join(buildCommand(targetOS, vmArguments, "build/outputs/"+targetOS+"/"+build.OutputBinaryName(pubspec.GetPubSpec().Name, targetOS)), " "), chownStr))
dockerRunCmd := exec.Command(build.DockerBin, args...)
dockerRunCmd.Stderr = os.Stderr
dockerRunCmd.Stdout = os.Stdout
dockerRunCmd.Dir = crossCompilingDir
Expand All @@ -252,7 +255,7 @@ func dockerBuild(projectName string, targetOS string, vmArguments []string) {
log.Infof("Successfully cross-compiled for " + targetOS)
}

func build(projectName string, targetOS string, vmArguments []string) {
func buildNormal(targetOS string, vmArguments []string) {
crossCompile = targetOS != runtime.GOOS
buildDocker = crossCompile || buildDocker

Expand All @@ -263,21 +266,21 @@ func build(projectName string, targetOS string, vmArguments []string) {
}

if !buildOmitFlutterBundle && !buildOmitEmbedder {
err := os.RemoveAll(outputDirectoryPath(targetOS))
err := os.RemoveAll(build.OutputDirectoryPath(targetOS))
log.Printf("Cleaning the build directory")
if err != nil {
log.Errorf("Failed to clean output directory %s: %v", outputDirectoryPath(targetOS), err)
log.Errorf("Failed to clean output directory %s: %v", build.OutputDirectoryPath(targetOS), err)
os.Exit(1)
}
}

err := os.MkdirAll(outputDirectoryPath(targetOS), 0775)
err := os.MkdirAll(build.OutputDirectoryPath(targetOS), 0775)
if err != nil {
log.Errorf("Failed to create output directory %s: %v", outputDirectoryPath(targetOS), err)
log.Errorf("Failed to create output directory %s: %v", build.OutputDirectoryPath(targetOS), err)
os.Exit(1)
}

cmdCheckFlutter := exec.Command(flutterBin, "--version")
cmdCheckFlutter := exec.Command(build.FlutterBin, "--version")
cmdCheckFlutterOut, err := cmdCheckFlutter.Output()
if err != nil {
log.Warnf("Failed to check your flutter channel: %v", err)
Expand All @@ -301,8 +304,8 @@ func build(projectName string, targetOS string, vmArguments []string) {
trackWidgetCreation = "--track-widget-creation"
}

cmdFlutterBuild := exec.Command(flutterBin, "build", "bundle",
"--asset-dir", filepath.Join(outputDirectoryPath(targetOS), "flutter_assets"),
cmdFlutterBuild := exec.Command(build.FlutterBin, "build", "bundle",
"--asset-dir", filepath.Join(build.OutputDirectoryPath(targetOS), "flutter_assets"),
"--target", buildTarget,
trackWidgetCreation,
)
Expand All @@ -328,7 +331,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
engineFile = "flutter_engine.dll"
}

outputEngineFile := filepath.Join(outputDirectoryPath(targetOS), engineFile)
outputEngineFile := filepath.Join(build.OutputDirectoryPath(targetOS), engineFile)
err = copy.Copy(
filepath.Join(engineCachePath, engineFile),
outputEngineFile,
Expand All @@ -347,19 +350,19 @@ func build(projectName string, targetOS string, vmArguments []string) {

err = copy.Copy(
filepath.Join(engineCachePath, "artifacts", "icudtl.dat"),
filepath.Join(outputDirectoryPath(targetOS), "icudtl.dat"),
filepath.Join(build.OutputDirectoryPath(targetOS), "icudtl.dat"),
)
if err != nil {
log.Errorf("Failed to copy icudtl.dat: %v", err)
os.Exit(1)
}

err = copy.Copy(
filepath.Join(buildPath, "assets"),
filepath.Join(outputDirectoryPath(targetOS), "assets"),
filepath.Join(build.BuildPath, "assets"),
filepath.Join(build.OutputDirectoryPath(targetOS), "assets"),
)
if err != nil {
log.Errorf("Failed to copy %s/assets: %v", buildPath, err)
log.Errorf("Failed to copy %s/assets: %v", build.BuildPath, err)
os.Exit(1)
}

Expand All @@ -375,7 +378,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
}

if buildBranch == "" {
currentTag, err := versioncheck.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
currentTag, err := versioncheck.CurrentGoFlutterTag(filepath.Join(wd, build.BuildPath))
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
Expand All @@ -399,7 +402,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
} else {
// when the buildBranch is empty and the currentTag is a release.
// Check if the 'go-flutter' needs updates.
versioncheck.CheckForGoFlutterUpdate(filepath.Join(wd, buildPath), currentTag)
versioncheck.CheckForGoFlutterUpdate(filepath.Join(wd, build.BuildPath), currentTag)
}

} else {
Expand All @@ -420,13 +423,13 @@ func build(projectName string, targetOS string, vmArguments []string) {
if crossCompile {
log.Infof("Because %s is not able to compile for %s out of the box, a cross-compiling container is used", runtime.GOOS, targetOS)
}
dockerBuild(projectName, targetOS, vmArguments)
buildInDocker(targetOS, vmArguments)
return
}

buildCommandString := buildCommand(targetOS, vmArguments, outputBinaryPath(projectName, targetOS))
buildCommandString := buildCommand(targetOS, vmArguments, build.OutputBinaryPath(pubspec.GetPubSpec().Name, targetOS))
cmdGoBuild := exec.Command(buildCommandString[0], buildCommandString[1:]...)
cmdGoBuild.Dir = filepath.Join(wd, buildPath)
cmdGoBuild.Dir = filepath.Join(wd, build.BuildPath)
cmdGoBuild.Env = append(os.Environ(),
buildEnv(targetOS, engineCachePath)...,
)
Expand Down Expand Up @@ -482,7 +485,7 @@ func buildEnv(targetOS string, engineCachePath string) []string {
}

func buildCommand(targetOS string, vmArguments []string, outputBinaryPath string) []string {
currentTag, err := versioncheck.CurrentGoFlutterTag(buildPath)
currentTag, err := versioncheck.CurrentGoFlutterTag(build.BuildPath)
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
Expand All @@ -506,9 +509,9 @@ func buildCommand(targetOS string, vmArguments []string, outputBinaryPath string
" -X github.com/go-flutter-desktop/go-flutter.PlatformVersion=%s "+
" -X github.com/go-flutter-desktop/go-flutter.ProjectName=%s "+
" -X github.com/go-flutter-desktop/go-flutter.ProjectOrganizationName=%s",
getPubSpec().Version,
pubspec.GetPubSpec().Version,
currentTag,
getPubSpec().Name,
pubspec.GetPubSpec().Name,
androidOrganizationName()))

outputCommand := []string{
Expand Down
Loading