Skip to content

Commit

Permalink
feat: add support for macOS
Browse files Browse the repository at this point in the history
Use build tags to add support for macOS
  • Loading branch information
rockavoldy authored Oct 27, 2023
2 parents c1b326e + 3f25f86 commit 004207f
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ jobs:
name: publish-binaries
path: ${{ github.workspace }}/publish

- name: Get Commits since last Release
id: changes
uses: simbo/changes-since-last-release-action@v1
with:
line-prefix: "* "
include-hashes: false

- name: Publish GitHub Release
if: needs.build.outputs.bump_tag != 'NULL'
uses: ncipollo/[email protected]
Expand All @@ -144,3 +151,6 @@ jobs:
artifacts: "${{ github.workspace }}/publish/*.zip"
tag: "${{ needs.build.outputs.bump_tag }}"
token: ${{ secrets.GITHUB_TOKEN }}
body: |
Changes since ${{ steps.changes.outputs.last-tag }}:
${{ steps.changes.outputs.log }}
192 changes: 192 additions & 0 deletions cmd/init_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package cmd

import (
"fmt"
"odoo-one-click/config"
"odoo-one-click/utils"
"os"
"os/exec"
"strings"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
Use: "init",
Short: "First initialization to install pyenv, and configure postgresql",
Long: "First initialization to install pyenv, and configure postgresql for local development",
Run: func(cmd *cobra.Command, args []string) {
CheckRequirement()
checkPyenv()
},
}

func CheckRequirement() {
// Check requirement for ubuntu and derivatives
fmt.Println("Checking requirement for macOS")

err := exec.Command("which", "brew").Run()
if err != nil {
Logger.Println("Check brew command: ", err)
}

listOfDeps := []string{"jpeg", "postgresql", "zlib"}

notInstalledDeps := make([]string, 0)

for _, dep := range listOfDeps {
err := exec.Command("brew", "list", dep).Run()
// when the package already installed, err will be nil
if err != nil {
Logger.Println("Check dependencies: ", err)
notInstalledDeps = append(notInstalledDeps, dep)
}
}

if len(notInstalledDeps) > 0 {
fmt.Println("Seems like you don't have all the dependencies installed, installing dependencies")

// brew didn't need to use sudo like on linux, so can just directly run the brew command
fmt.Println("Update apt repositories, please wait...")
err = exec.Command("brew", "update").Run()
if err != nil {
Logger.Fatalln("Failed to update brew: ", err)
}

fmt.Println("Installing dependencies, please wait...")
cmdAptInstall := utils.PrependCommand(notInstalledDeps, []string{"brew", "install"})

err = exec.Command("sudo", cmdAptInstall...).Run()
if err != nil {
Logger.Fatalln("Install dependencies: ", err)
}

fmt.Println("Dependencies installed")
err = exec.Command("brew", "services", "start", "postgresql").Run()
if err != nil {
Logger.Fatalln("Failed to start postgresql: ", err)
}

}

_, err = checkDBAccess()
if err != nil {
if strings.Contains(err.Error(), "exit status 127") {
Logger.Fatalln("Postgresql is not installed, please install it first")
}

Logger.Fatalln("Can't access DB: ", err)
}

fmt.Printf("Database user '%s' with password '%s' created with superuser access\n", config.DBUsername(), config.DB_PASSWORD)

err = addBuildPrerequisite()
if err != nil {
Logger.Fatalln("Failed to setup some requirements: ", err)
}

utils.PyenvInfoBash()
}

func checkDBAccess() (bool, error) {
os.Setenv("PGPASSWORD", config.DB_PASSWORD)
psqlCmd := fmt.Sprintf("psql -h %s -p %s -U %s -c 'SELECT 1'", config.DB_HOST, config.DB_PORT, config.DBUsername())

err := exec.Command("zsh", "-c", psqlCmd).Run()
if err != nil {
return false, err
}

Logger.Println("Database can be accessed")

return true, nil
}

func isPyenvInstalled() (bool, error) {
err := exec.Command("pyenv", "--version").Run()
if err != nil {
return false, err
}

return true, nil
}

func installPyenv() (bool, error) {
pyenvScript := exec.Command("curl", "https://pyenv.run")
runBash := exec.Command("zsh")
runBash.Stdin, _ = pyenvScript.StdoutPipe()
runBash.Stdout = os.Stdout
_ = runBash.Start()
_ = pyenvScript.Run()
err := runBash.Wait()
if err != nil {
return false, err
}

err = exec.Command("zsh", "-c", "exec $SHELL").Run()
if err != nil {
return false, err
}

return true, nil
}

func checkPyenv() {
pyenvInstalled, err := isPyenvInstalled()
if err != nil {
Logger.Println(err)
}

if !pyenvInstalled {
Logger.Println("Pyenv is not installed, installing pyenv")
_, err := installPyenv()
if err != nil {
Logger.Fatalf("Failed to install pyenv: %s", err)
}
}

if _, err := os.Stat(config.OdooDir()); os.IsNotExist(err) {
err = os.MkdirAll(config.OdooDir(), config.ODOO_PERMISSION)
if err != nil {
Logger.Fatalln(err)
}
}
}

func addBuildPrerequisite() error {
// some build flags and some brew packages need to be linked forcefully

// link jpeg and zlib first
err := exec.Command("brew", "link", "jpeg", "--force").Run()
if err != nil {
return err
}

err = exec.Command("brew", "link", "zlib", "--force").Run()
if err != nil {
return err
}

// add build flags to cppflags
// LDFLAGS=-L$(brew --prefix [email protected])/lib
err = exec.Command("export", "LDFLAGS=-L$(brew --prefix [email protected])/lib").Run()
if err != nil {
return err
}
// CPPFLAGS=-I$(brew --prefix [email protected])/include
err = exec.Command("export", "CPPFLAGS=-I$(brew --prefix [email protected])/include").Run()
if err != nil {
return err
}
// CFLAGS="-Wno-error=implicit-function-declaration"
err = exec.Command("export", "CFLAGS=\"-Wno-error=implicit-function-declaration\"").Run()
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/init.go → cmd/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var initCmd = &cobra.Command{

func CheckRequirement() {
// Check requirement for ubuntu and derivatives
fmt.Println("Checking requirement")
fmt.Println("Checking requirement for Ubuntu")

listOfDeps := []string{"build-essential", "postgresql", "postgresql-client", "libxml2-dev", "libssl-dev", "libffi-dev", "libxslt1-dev", "libldap2-dev", "libsasl2-dev", "libtiff5-dev", "libjpeg8-dev", "libopenjp2-7-dev", "zlib1g-dev", "libfreetype6-dev", "liblcms2-dev", "libwebp-dev", "libharfbuzz-dev", "libpq-dev", "curl", "wget", "git", "libsqlite3-dev", "libreadline-dev", "libbz2-dev", "tk-dev"}

Expand Down
10 changes: 9 additions & 1 deletion config/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"os"
"os/user"
"runtime"
)

var (
Expand All @@ -22,7 +24,13 @@ func OdooVersion() string {
}

func DBUsername() string {
return "odoo"
switch runtime.GOOS {
case "darwin":
u, _ := user.Current()
return u.Username
default:
return "odoo"
}
}

func OdooDir() string {
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (

func main() {
// OS Check, for now it's only for ubuntu and derivatives
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
if runtime.GOOS == "linux" {
// Check if UBUNTU_CODENAME is on allowedOS
if err := checkOSVersion(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else if runtime.GOOS == "darwin" {

} else {
fmt.Println("This application only works on Ubuntu or derivatives")
os.Exit(1)
Expand Down

0 comments on commit 004207f

Please sign in to comment.