1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 03:36:22 -07:00

Finish build.py

Also remove powershell script, modify bootstrap script to use build.py
This commit is contained in:
Eggbertx 2020-11-04 20:11:51 -08:00
parent 485ed84bac
commit 2e99788426
5 changed files with 224 additions and 226 deletions

137
build.ps1
View file

@ -1,137 +0,0 @@
#!/usr/bin/env pwsh
param (
[string]$action = "build",
[bool]$minify = $TRUE,
[string]$platform,
[string]$releaseFor
)
$ErrorActionPreference = "Stop"
if($releaseFor -ne "*" -And $releaseFor -ne "") {
$platform = $releaseFor
}
$BIN = "gochan"
$BINEXE = "gochan.exe"
if( $releaseFor -ne "*" ) {
$BINEXE = "$BIN$($env:GOOS=$platform; go env GOEXE)"
}
$GCOS = go env GOOS
$GCOS_NAME = $GCOS
if ($GCOS_NAME -eq "darwin") {
$GCOS_NAME = "macos"
}
$VERSION = Get-Content version
$RELEASE_NAME = "$BIN-v${VERSION}_$GCOS_NAME"
$RELEASE_DIR = "releases/${RELEASE_NAME}"
$LDFLAGS = "-X main.versionStr=${VERSION} -s"
$DOCUMENT_ROOT_FILES = @"
banned.jpg
notbanned.png
permabanned.jpg
favicon*
firstrun.html
hittheroad*
"@ -split "`n"
for ($l = 0; $l -lt $DOCUMENT_ROOT_FILES.Count; $l++) {
$line = "html/" + $DOCUMENT_ROOT_FILES[$l]
$DOCUMENT_ROOT_FILES[$l] = Get-ChildItem $line
}
$DOCUMENT_ROOT_FILES += @"
html/css
html/error
html/javascript
"@
function build {
$cmd = "& go build -v -gcflags=-trimpath=$PWD -asmflags=-trimpath=$PWD -ldflags=`"$LDFLAGS`" -o $BINEXE ./cmd/gochan "
$env:GOOS=$platform; Invoke-Expression $cmd
}
function clean {
Remove-Item $BIN*
Remove-Item releases/* -Force -Recurse
}
function dependencies {
go get -v `
github.com/disintegration/imaging `
github.com/nranchev/go-libGeoIP `
github.com/go-sql-driver/mysql `
github.com/lib/pq `
golang.org/x/net/html `
github.com/aquilax/tripcode `
golang.org/x/crypto/bcrypt `
github.com/frustra/bbcode `
github.com/tdewolff/minify `
github.com/mojocn/base64Captcha
}
function dockerImage {
throw "Docker image creation not yet implemented"
# docker build . -t="eggbertx/gochan"
}
function release {
clean
mkdir -p `
$RELEASE_DIR/html `
$RELEASE_DIR/log `
cp LICENSE $RELEASE_DIR
cp README.md $RELEASE_DIR
cp -r sample-configs $RELEASE_DIR
cp -r templates $RELEASE_DIR
foreach($line in $DOCUMENT_ROOT_FILES) {
$arr = $line.Split()
foreach($word in $arr) {
Copy-Item -Recurse $word $RELEASE_DIR/html
}
}
$env:GOOS=$platform; ./build.ps1
}
function doSass {
if($minify) {
sass --style compressed --no-source-map sass:html/css
} else {
sass --no-source-map sass:html/css
}
}
switch ($action) {
"build" {
build
}
"clean" {
clean
}
"dependencies" {
dependencies
}
"js" {
throw "Frontend transpilation coming soon"
}
"release" {
if($releaseFor -eq "*") {
./build.ps1 -action release -platform darwin -releaseFor darwin
./build.ps1 -action release -platform linux -releaseFor linux
./build.ps1 -action release -platform windows -releaseFor windows
} else {
release
}
}
"sass" {
doSass
}
"test" {
go test -v ./src
}
Default {
Write-Output "Invalid or unsupported command"
}
}

296
build.py
View file

@ -5,30 +5,78 @@
import argparse
import os
from os import path
import shutil
import subprocess
import sys
import tarfile
from zipfile import ZipFile
gc_dependencies = [
"github.com/disintegration/imaging ",
"github.com/nranchev/go-libGeoIP ",
"github.com/go-sql-driver/mysql ",
"github.com/lib/pq ",
"golang.org/x/net/html ",
"github.com/aquilax/tripcode ",
"golang.org/x/crypto/bcrypt ",
"github.com/frustra/bbcode ",
"github.com/tdewolff/minify ",
"github.com/disintegration/imaging",
"github.com/nranchev/go-libGeoIP",
"github.com/go-sql-driver/mysql",
"github.com/lib/pq",
"golang.org/x/net/html",
"github.com/aquilax/tripcode",
"golang.org/x/crypto/bcrypt",
"github.com/frustra/bbcode",
"github.com/tdewolff/minify",
"github.com/mojocn/base64Captcha"
]
release_files = [
"html/banned.jpg",
"html/css",
"html/error",
"html/favicon2.png",
"html/favicon.png",
"html/firstrun.html",
"html/hittheroad.mp3",
"html/hittheroad.ogg",
"html/hittheroad.wav",
"html/javascript",
"html/notbanned.png",
"html/permabanned.jpg",
"sample-configs",
"sql",
"templates",
"initdb_master.sql",
"initdb_mysql.sql",
"initdb_postgres.sql",
"LICENSE",
"README.md",
]
def fs_action(action, sourcefile, destfile = ""):
isfile = path.isfile(sourcefile) or path.islink(sourcefile)
isdir = path.isdir(sourcefile)
if action == "copy":
fs_action("delete", destfile)
if isfile:
shutil.copy(sourcefile, destfile)
elif isdir:
shutil.copytree(sourcefile, destfile)
elif action == "move":
fs_action("delete", destfile)
shutil.move(sourcefile, destfile)
elif action == "mkdir":
if isfile:
fs_action("delete", sourcefile)
elif not isdir:
os.makedirs(sourcefile)
elif action == "delete":
if isfile:
os.remove(sourcefile)
elif isdir:
shutil.rmtree(sourcefile)
else:
raise Exception("Invalid action, must be 'copy', 'move', 'mkdir', or 'delete'")
def run_cmd(cmd, print_output = True, realtime = False, print_command = False):
if print_command:
print(cmd)
use_stdout = subprocess.PIPE
if print_output == False:
use_stdout = open(os.devnull, 'w')
proc = subprocess.Popen(cmd, stdout = use_stdout, stderr=subprocess.STDOUT, shell = True)
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr=subprocess.STDOUT, shell = True)
output = ""
status = 0
if realtime: # print the command's output in real time, ignores print_output
@ -53,20 +101,62 @@ def run_cmd(cmd, print_output = True, realtime = False, print_command = False):
status = 0
return (output, status)
def set_vars(goos = ""):
""" Sets version and GOOS-related variables to be used globally"""
global gcos
global gcos_name # used for release, since macOS GOOS is "darwin"
global exe
global gochan_bin
global gochan_exe
global migration_bin
global migration_exe
global version
if goos != "":
os.environ["GOOS"] = goos
gcos, gcos_status = run_cmd("go env GOOS", print_output = False)
exe, exe_status = run_cmd("go env GOEXE", print_output = False)
if gcos_status + exe_status != 0:
print("Invalid GOOS value, check your GOOS environment variable")
exit(1)
gcos_name = gcos
if gcos_name == "darwin":
gcos_name = "macos"
gochan_bin = "gochan"
gochan_exe = "gochan" + exe
migration_bin = "gochan-migration"
migration_exe = "gochan-migration" + exe
version_file = open("version", "r")
version = version_file.read().strip()
version_file.close()
def build(debugging = False):
use_cmd = build_cmd
pwd = os.getcwd()
trimpath = "-trimpath=" + pwd
gcflags = " -gcflags=\"" + trimpath + "{}\""
ldflags = " -ldflags=\"-X main.versionStr=" + version + "{}\""
build_cmd = "go build -v -asmflags=" + trimpath
if debugging:
use_cmd = dbg_build_cmd
print("Building for", gcos,"with debugging symbols")
gcflags = gcflags.format(" -l -N")
ldflags = ldflags.format("")
else:
ldflags = ldflags.format(" -w -s")
gcflags = gcflags.format("")
print("Building for", gcos)
status = run_cmd(use_cmd + " -o " + gochan_exe + " ./cmd/gochan", realtime = True, print_command = True)[1]
build_cmd += gcflags + ldflags
status = run_cmd(build_cmd + " -o " + gochan_exe + " ./cmd/gochan", realtime = True, print_command = True)[1]
if status != 0:
print("Failed building gochan, got status code", status)
exit(1)
status = run_cmd(use_cmd + " -o " + migration_exe + " ./cmd/gochan-migration", realtime = True, print_command = True)[1]
status = run_cmd(build_cmd + " -o " + migration_exe + " ./cmd/gochan-migration", realtime = True, print_command = False)[1]
if status != 0:
print("Failed building gochan-migration, got status code", status)
exit(1)
@ -74,29 +164,66 @@ def build(debugging = False):
def clean():
print("Cleaning up")
del_files = ["gochan", "gochan.exe", "gochan-migration", "gochan-migration.exe"]
del_files = ["gochan", "gochan.exe", "gochan-migration", "gochan-migration.exe", "releases/", "pkg/gclog/logtest/"]
for del_file in del_files:
if path.exists(del_file):
os.remove(del_file)
fs_action("delete", del_file)
def dependencies():
for dep in gc_dependencies:
run_cmd("go get -v " + dep, realtime = True)
run_cmd("go get -v " + dep, realtime = True, print_command = True)
def docker(option = "guestdb"):
cmd = ""
cmd = "docker-compose -f {} up --build"
if option == "guestdb":
cmd = "docker-compose -f docker/docker-compose-mariadb.yaml up --build"
cmd = cmd.format("docker/docker-compose-mariadb.yaml")
elif option == "hostdb":
cmd = "docker-compose -f docker/docker-compose.yml.default up --build"
cmd = cmd.format("docker/docker-compose.yml.default")
elif option == "macos":
cmd = "docker-compose -f docker/docker-compose-syncForMac.yaml up --build"
cmd = cmd.format("docker/docker-compose-syncForMac.yaml")
status = run_cmd(cmd, print_output = True, realtime = True, print_command = True)[1]
if status != 0:
print("Failed starting a docker container, exited with status code", status)
def install():
pass
def install(prefix = "/usr", document_root = "/srv/gochan"):
if gcos == "windows" or gcos == "darwin":
print("Installation is not currently supported for Windows and macOS, use the respective directory created by running `python build.py release`")
exit(1)
fs_action("mkdir", "/etc/gochan")
fs_action("mkdir", path.join(prefix, "share/gochan"))
fs_action("mkdir", document_root)
fs_action("mkdir", "/var/log/gochan")
for file in release_files:
out_path = path.join(prefix, "share", file)
if file.startswith("html/"):
out_path = path.join(document_root, file.replace("html/", ""))
print("Installing", file, "to", out_path)
fs_action("copy", file, out_path)
# print("installing sample-configs/gochan.example.json to /etc/gochan/gochan.example.json")
# fs_action("copy")
if(path.exists(gochan_exe) == False or path.exists(migration_exe)):
build()
print("Installing",gochan_exe,"to",path.join(prefix, "bin", gochan_exe))
fs_action("copy", gochan_exe, path.join(prefix, "bin", gochan_exe))
print("Installing",migration_exe,"to",path.join(prefix, "bin", migration_exe))
fs_action("copy", migration_exe, path.join(prefix, "bin", migration_exe))
print(
"gochan successfully installed. If you haven't already, you should copy\n",
"sample-configs/gochan.example.json to /etc/gochan/gochan.json and modify that as needed."
)
if gcos == "linux":
print("If your Linux distribution has systemd, you will also need to run the following commands:\n",
"cp sample-configs/gochan-[mysql|postgresql].service /lib/systemd/system/gochan.service\n",
"systemctl daemon-reload\n",
"systemctl enable gochan.service\n",
"systemctl start gochan.service",
)
def js(minify = False, watch = False):
print("Transpiling JS")
@ -105,107 +232,108 @@ def js(minify = False, watch = False):
npm_cmd += "-minify"
if watch:
npm_cmd += "-watch"
status = run_cmd(npm_cmd, True, True, True)[1]
if status != 0:
print("JS transpiling failed with status", status)
def release(all = True):
global gcos
print("Creating releases for GOOS", gcos)
def release(goos):
set_vars(goos)
build(False)
release_name = gochan_bin + "-v" + version + "_" + gcos_name
release_dir = path.join("releases", release_name)
print("Creating release for", gcos_name)
handle = None
func = None
if goos == "windows" or goos == "darwin":
handle = ZipFile(release_dir + ".zip", "w")
func = handle.write
else:
handle = tarfile.open(release_dir + ".tar.gz", "w:gz")
func = handle.add
func(gochan_exe, path.join(release_name, gochan_exe))
func(migration_exe, path.join(release_name, migration_exe))
for file in release_files:
func(file, path.join(release_name, file))
handle.close()
def sass(minify = False):
sass_cmd = "sass "
if minify:
sass_cmd += "--style compressed "
status = run_cmd(sass_cmd + "--no-source-map sass:html/css", realtime = True, print_command = True)[1]
sass_cmd += "--no-source-map sass:html/css"
status = run_cmd(sass_cmd, realtime = True, print_command = True)[1]
if status != 0:
print("Failed running sass with status", status)
def test():
run_cmd("go test ./pkg/gcutil/", realtime = True, print_command = True)
pkgs = os.listdir("pkg")
for pkg in pkgs:
run_cmd("go test " + path.join("./pkg", pkg), realtime = True, print_command = True)
if __name__ == "__main__":
global gcos
global gcos_name # used for release, since macOS GOOS is "darwin"
global exe
global gochan_bin
global gochan_exe
global migration_bin
global migration_exe
global build_cmd
global dbg_build_cmd
action = "build"
try:
sys.platform
action = sys.argv.pop(1)
except Exception: # no argument was passed
pass
if(action.startswith("-") == False):
sys.argv.insert(1, action)
set_vars()
valid_actions = ["build", "clean", "dependencies", "docker", "install", "js", "release", "sass", "test"]
parser = argparse.ArgumentParser(description = "gochan build script")
parser.add_argument("action",
nargs = 1,
default = "build",
choices = valid_actions
)
parser.add_argument("action", nargs = 1, default = "build", choices = valid_actions)
if action == "--help" or action == "-h":
parser.print_help()
exit(2)
gcos, gcos_status = run_cmd("go env GOOS")
exe, exe_status = run_cmd("go env GOEXE")
if gcos_status + exe_status != 0:
print("Invalid GOOS value, check your GOOS environment variable")
exit(1)
gochan_bin = "gochan"
gochan_exe = "gochan" + exe
migration_exe = "gochan-migration" + exe
version_file = open("version", "r")
version = version_file.read().strip()
version_file.close()
pwd = os.getcwd()
trimpath = "-trimpath=" + pwd
ldflags = "-X main.versionStr=" + version
build_prefix = "go build -v -asmflags=" + trimpath + " -gcflags="
build_cmd = build_prefix + trimpath + " -ldflags=\"" + ldflags + " -w -s\""
dbg_build_cmd = build_prefix + "\"" + trimpath + " -l -N\" -ldflags=\"" + ldflags + "\""
if action == "build":
parser.add_argument("--debug", type = bool, default = False, nargs = "?")
parser.add_argument("--debug", help = "build gochan and gochan-frontend with debugging symbols", action = "store_true")
args = parser.parse_args()
build(args.debug is not False)
build(args.debug)
elif action == "clean":
clean()
exit(0)
elif action == "dependencies":
dependencies()
elif action == "docker":
parser.add_argument("--option", type = str, default = "guestdb", choices = ["guestdb", "hostdb", "macos"])
parser.add_argument("--option",
default = "guestdb", choices = ["guestdb", "hostdb", "macos"],
help = "create a Docker container, see docker/README.md for more info"
)
args = parser.parse_args()
docker(args.option)
elif action == "install":
install()
parser.add_argument("--prefix",
default = "/usr",
help = "install gochan to this directory and its subdirectories",
)
parser.add_argument("--documentroot",
default = "/srv/gochan",
help = "install files in ./html/ to this directory to be requested by a browser"
)
args = parser.parse_args()
install(args.prefix, args.documentroot)
elif action == "js":
parser.add_argument("--minify", type = bool, default = False, nargs = "?")
parser.add_argument("--watch", type = bool, default = False, nargs = "?")
parser.add_argument("--minify", action = "store_true", help = "create a minified gochan.js")
parser.add_argument("--watch", action = "store_true", help = "automatically rebuild when you change a file (keeps running)")
args = parser.parse_args()
js(args.minify is not False, args.watch is not False)
js(args.minify, args.watch)
elif action == "release":
parser.add_argument("--all", type = bool, default = False, nargs = "?")
parser.add_argument("--all", help = "build releases for Windows, macOS, and Linux", action = "store_true")
args = parser.parse_args()
release(args.all is not False)
fs_action("mkdir", "releases")
if args.all:
release("windows")
release("darwin")
release("linux")
else:
release(gcos)
elif action == "sass":
parser.add_argument("--minify", type = bool, default = False, nargs = "?")
parser.add_argument("--minify", action = "store_true")
args = parser.parse_args()
sass(args.minify is not False)
sass(args.minify)
elif action == "test":
test()

2
go.mod
View file

@ -15,5 +15,5 @@ require (
github.com/tdewolff/parse v2.3.4+incompatible // indirect
github.com/tdewolff/test v1.0.6 // indirect
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
golang.org/x/net v0.0.0-20201022231255-08b38378de70
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102
)

4
go.sum
View file

@ -41,6 +41,10 @@ golang.org/x/net v0.0.0-20201010224723-4f7140c49acb h1:mUVeFHoDKis5nxCAzoAi7E8Gh
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201022231255-08b38378de70 h1:Z6x4N9mAi4oF0TbHweCsH618MO6OI6UFgV0FP5n0wBY=
golang.org/x/net v0.0.0-20201022231255-08b38378de70/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201024042810-be3efd7ff127 h1:pZPp9+iYUqwYKLjht0SDBbRCRK/9gAXDy7pz5fRDpjo=
golang.org/x/net v0.0.0-20201024042810-be3efd7ff127/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 h1:42cLlJJdEh+ySyeUUbEQ5bsTiq8voBeTuweGVkY6Puw=
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View file

@ -62,7 +62,7 @@ else
exit 1
fi
apt-get -y install git subversion mercurial nginx ffmpeg make golang-1.11
apt-get -y install git subversion mercurial nginx ffmpeg golang-1.11
ln -s /usr/lib/go-1.11/bin/* /usr/local/bin/
@ -131,10 +131,13 @@ python build_initdb.py
cd ..
mkdir -p $GOPATH/src/github.com/gochan-org/gochan
cp -r pkg $GOPATH/src/github.com/gochan-org/gochan
make dependencies
make
./build.py dependencies
./build.py
EOF
# make install
cd /vagrant
./build.py install
/vagrant/gochan -rebuild all
# if [ -d /lib/systemd ]; then
# systemctl start gochan.service