neo-layout/windows/neo-vars/tools/build-configure.ahk

205 lines
6.0 KiB
AutoHotkey

; Configure future builds
;
; Usage:
; When included in another script, the function "build_configure" is provided.
; When running as a standalone script, build_configuration is run with defaults.
;
; Requires AutoHotkey_L (Unicode-Version of AutoHotkey).
#Include %A_LineFile%\..\script\logwindow.ahk
#Include %A_LineFile%\..\script\util.ahk
; -------------------------------------
; Configure Build
; -------------------------------------
; Reads user-defined configuration fom 'iniFile'.
; Updates include-scripts in 'buildIncludeFile' which should be included
; by the sources and parsed by the compiler during the build process.
;
; Arguments:
;
; srcDir, binDir, configDir
; Sources-, (output-) binaries-, and configuration-files directory
; iniFile
; Name of an ini-file from which to read the build configuration.
; buildIncludeFile
; Name of the ahk-script file that is overwritten with build-specific
; commands and declarations.
;
; Relative paths are understood relative to the current working directory.
; Supported ini-keys: portable, custom, customName
; (for details, see "..\config\build-config.ini").
;
; Result:
; An associative array with the configurations made.
; Keys: portable, custom, customName, revision, exeName
;
; Notes:
; Prints messages to a logging window, which shall be initialized on entry.
; Stops on errors with exit code 1.
;
build_configure(srcDir:="", binDir:="", configDir:="", iniFile:="", buildIncludeFile:="")
{
; Default paths (absolute so we don't rely on working dir.)
if not srcDir
srcDir := A_ScriptDir . "\..\src"
if not binDir
binDir := A_ScriptDir . "\..\bin"
configDir := A_ScriptDir . "\..\config"
if not iniFile
iniFile := util_getFullPath(configDir . "\build-config.ini")
if not buildIncludeFile
buildIncludeFile := util_getFullPath(srcDir . "\config-iscompiled1.generated.ahk")
;
; Read user-defined configuration
;
; Default configs (keys are not case-sensitive)
userConfigDefaults := {portable: 0, custom: 0, customName: "", ahk2ExeParams: "", ahk2ExePath: ""}
configs := {revision: "<unknown>", exeName: ""}
; Read user-defined configs from ini-file
logEntry("Read user-defined configuration...")
if not FileExist(iniFile)
{
logError("Configuration file not found at '" . iniFile . "'")
logFinal()
}
for key, value in userConfigDefaults
{
IniRead, valueNew, %iniFile%, Global, %key% , %value%
configs[key] := valueNew
}
;
; Other configurations
;
; Try to obtain revsion information (requires git)
logEntry("Query revision information...")
exitCode := util_runWaitCMD("git rev-parse HEAD", strOut, strErr)
if not exitCode and StrLen(strOut) >= 7
configs["revision"] := SubStr(strOut, 1, 7)
else
logWarning("Could not query revision information from git repository. " . strErr)
; Output file name
logEntry("Configure executable filename...")
exeName := "neo20"
if (configs["customName"])
{
; Strip to filename if path provided
SplitPath, % configs["customName"] , customNameStripped
; Add userdefined string "-<customName>"
exeName .= "-" . customNameStripped
}
else
{
; Add "-custom" if exe includes custom script
if configs["custom"]
exeName .= "-custom"
; Add "-portable" if exe is portable
if configs["portable"]
exeName .= "-portable"
; Add revision info "-r<revision>" if uncommitted changes were made
; TODO: restrict diff to compose- and neovars subtrees (?)
exitCode := util_runWaitCMD("git diff --quiet")
if exitCode ; git index or working-tree not clean, or other failure
exeName .= "-r" . configs["revision"]
}
exeName .= ".exe"
; Normalized path is used by the compiler to check against running processes
configs["exeName"] := util_getFullPath(binDir . "\" . exeName)
;
; Update build-specific inlude file
;
logEntry("Update build-specific includes in '" . buildIncludeFile . "'...")
tmpFile := buildIncludeFile . ".tmp"
; Clear old tmp. files (if exist)
FileDelete, % tmpFile
if FileExist(tmpFile)
{
logError("Could not delete old configuration file '" . tmpFile . "'")
logFinal()
}
; Write include-file line-by-line
scriptGeneratedBy := A_ScriptName
scriptLinePortable := "isPortable := " . configs["portable"]
scriptLineCustom := (configs["custom"] ? "" : ";") . "#include *i %A_AppData%\Neo2\custom.ahk"
scriptLineCustomName := "customName := """ . configs["customName"] . """"
scriptLineRevision := "revision := """ . configs["revision"] . """"
scriptLineExeName := ";@Ahk2Exe-ExeName " . configs["exeName"]
FileAppend,
(LTRIM
; -*- encoding: utf-8 -*-
;
; ** THIS FILE WAS GENERATED BY %scriptGeneratedBy% **
; ** DO NOT EDIT BY HAND -- FILE MAY BE OVERWRITTEN ANYTIME **
;
; This file is included in compiled scripts only
;
; Run portable version?
%scriptLinePortable%
; Include user-defined scripts?
%scriptLineCustom%
; Modified name for executable
%scriptLineCustomName%
; Revision information
%scriptLineRevision%
; Binaries output path
%scriptLineExeName%
), % tmpFile, UTF-8
if ErrorLevel
{
logError("Failed to write the new configuration to '" . tmpFile . "'...")
logFinal()
}
; Overwrite old file with new
FileMove, % tmpFile, % buildIncludeFile, 1
if ErrorLevel
{
logError("Could not replace the old configuration in '" . buildIncludeFile . "'...")
logFinal()
}
; Return all configurations to caller
Return configs
}
; -------------------------------------
; Run as standalone
; -------------------------------------
if (A_LineFile == A_ScriptFullPath)
{
logInit("Build-configuration")
; Make sure AHK runs in Unicode version
if (not A_IsUnicode)
{
logError("Unicode support missing! Restart this script with the Unicode-version of AutoHotkey, e.g. through AutoHotkeyU32.exe or AutoHotkeyU64.exe.")
logFinal()
}
; Run with defaults
build_configure(srcDir, binDir, iniFile, buildIncludeFile)
logEntry("Build-configuration completed.")
logFinal()
}