Skip to content
Snippets Groups Projects
Commit ce1ef6fd authored by Pregmodder's avatar Pregmodder
Browse files

Merge branch 'gulp-fix' into 'pregmod-master'

Replace all twine legacy scripts with Gulp task

See merge request !9103
parents 125820ff 3d221a30
No related branches found
No related tags found
1 merge request!9103Replace all twine legacy scripts with Gulp task
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Task to concat files injecting their names in between -->
<UsingTask TaskName="ConcatFiles" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Inputs ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<BaseDir ParameterType="System.String" Required="true" />
<Output ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Code Type="Fragment" Language="cs"><![CDATA[
using (StreamWriter output = new StreamWriter(Output)) {
int bdl = BaseDir.Length;
foreach (ITaskItem item in Inputs) {
string inp = item.GetMetadata("FullPath");
string rp = inp.StartsWith(BaseDir) ? inp.Substring(bdl) : inp; // a simple relative path
output.WriteLine("/* Source: {0} */", rp);
output.Write(File.ReadAllText(inp));
output.WriteLine();
}
}
]]></Code>
</Task>
</UsingTask>
<!-- https://stackoverflow.com/questions/7837644/how-to-replace-string-in-file-using-msbuild -->
<!-- Unused
<UsingTask TaskName="ReplaceFileText" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
<MatchExpression ParameterType="System.String" Required="true" />
<ReplacementText ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
);
]]>
</Code>
</Task>
</UsingTask>
-->
<!-- https://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689 -->
<UsingTask TaskName="StripComments" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var blockComments = @"/\*(.*?)\*/";
var lineComments = @"//(.*?)\r?\n";
var strings = @"""((\\[^\n]|[^""\n])*)""";
var verbatimStrings = @"@(""[^""]*"")+";
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename),
blockComments + "|" + lineComments + "|" + strings + "|" + verbatimStrings,
me => {
if (me.Value.StartsWith("/*") || me.Value.StartsWith("//"))
return me.Value.StartsWith("//") ? "\n" : "";
// Keep the literal strings
return me.Value;
},
RegexOptions.Singleline
)
);
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="GetToolPath" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<ToolName Required="true" />
<LocalPath />
<ToolPath Output="true" />
<ToolFound ParameterType="System.Boolean" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="Microsoft.Win32.Registry" />
<Code Type="Class" Language="cs" Source="$(MSBuildThisFileDirectory)FindToolPath.cs" />
</Task>
</UsingTask>
<Target Name="FindGitWindows" Condition=" '$([MSBuild]::IsOsPlatform(Windows))' ">
<GetToolPath ToolName="git">
<Output TaskParameter="ToolPath" PropertyName="GitExe"/>
<Output TaskParameter="ToolFound" PropertyName="GitExeFound"/>
</GetToolPath>
</Target>
<Target Name="FindGitUnix" Condition=" '$([MSBuild]::IsOSUnixLike())' " >
<PropertyGroup>
<GitExe>git</GitExe>
</PropertyGroup>
<Exec Command="$(GitExe) help > /dev/null" IgnoreExitCode="True">
<Output TaskParameter="ExitCode" PropertyName="GitExeExitCode" />
</Exec>
<PropertyGroup>
<GitExeFound Condition=" '$(GitExeExitCode)' == '0' " >true</GitExeFound>
<GitExeFound Condition=" '$(GitExeExitCode)' != '0' " >false</GitExeFound>
</PropertyGroup>
</Target>
<Target Name="FindGit" DependsOnTargets="FindGitWindows;FindGitUnix" />
</Project>
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;
/*
Copyright (c) 2014, LoreSoft
This class was taken from https://github.com/loresoft/msbuildtasks/ and is licensed under BSD-2 license
*/
internal static class ToolPathUtil
{
public static bool SafeFileExists(string path, string toolName)
{
return SafeFileExists(Path.Combine(path, toolName));
}
public static bool SafeFileExists(string file)
{
Console.WriteLine(String.Format("Testing {0}", file));
try { return File.Exists(file); }
catch { } // eat exception
return false;
}
public static string MakeToolName(string name)
{
return (Environment.OSVersion.Platform == PlatformID.Unix) ?
name : name + ".exe";
}
public static string FindInRegistry(string toolName)
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + toolName, false);
if (key != null)
{
string possiblePath = key.GetValue(null) as string;
if (SafeFileExists(possiblePath))
return Path.GetDirectoryName(possiblePath);
}
}
catch (System.Security.SecurityException) { }
return null;
}
public static string FindInPath(string toolName)
{
string pathEnvironmentVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
string[] paths = pathEnvironmentVariable.Split(new[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
foreach (string path in paths)
{
if (SafeFileExists(path, toolName))
{
return path;
}
}
return null;
}
public static string FindInProgramFiles(string toolName, params string[] commonLocations)
{
foreach (string location in commonLocations)
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), location);
if (SafeFileExists(path, toolName))
{
return path;
}
}
return null;
}
public static string FindInLocalPath(string toolName, string localPath)
{
if (localPath == null)
return null;
string path = new DirectoryInfo(localPath).FullName;
if (SafeFileExists(localPath, toolName))
{
return path;
}
return null;
}
}
public class GetToolPath : Microsoft.Build.Utilities.Task
{
public virtual string ToolName { get; set; }
public virtual string LocalPath { get; set; }
public virtual string ToolPath { get; set; }
public virtual bool Success { get; set; }
public override bool Execute()
{
this.ToolPath = null;
this.Success = false;
string tool = ToolPathUtil.MakeToolName(ToolName);
string toolDir =
ToolPathUtil.FindInPath(tool) ??
ToolPathUtil.FindInRegistry(tool) ??
ToolPathUtil.FindInProgramFiles(tool, tool) ??
ToolPathUtil.FindInProgramFiles(tool, Path.Combine(tool, "bin")) ??
ToolPathUtil.FindInLocalPath(tool, LocalPath);
if (toolDir == null)
{
throw new Exception(String.Format("Could not find {0}. Looked in PATH locations and various common folders inside Program Files as well as LocalPath.", tool));
}
Console.WriteLine("Found {0} at {1}", tool, toolDir);
ToolPath = Path.Combine(toolDir, tool);
Success = true;
return Success;
}
}
@echo off
:: Generates devNotes/twineCSS.txt from all .css files in src/ subdir
:: See if we can find a git installation
setlocal enabledelayedexpansion
for %%k in (HKCU HKLM) do (
for %%w in (\ \Wow6432Node\) do (
for /f "skip=2 delims=: tokens=1*" %%a in ('reg query "%%k\SOFTWARE%%wMicrosoft\Windows\CurrentVersion\Uninstall\Git_is1" /v InstallLocation 2^> nul') do (
for /f "tokens=3" %%z in ("%%a") do (
set GIT=%%z:%%b
set GITFOUND=yes
goto FOUND
)
)
)
)
:FOUND
if %GITFOUND% == yes (
set "PATH=%GIT%bin;%PATH%"
bash --login -c ./makeTwineCSSPassage.sh
)
#!/bin/sh
# Generates devNotes/twine CSS.txt from all .css files in src/ subdir
# Joins all .css files from the current dir (recursive) into a Twee [script] passage
# arguments:
# $1: root repo dir
# $2: output file name
collectCSSForTwine() {
local files=$(find . -iname '*.css' -print)
files=$(echo "$files" | sort)
echo "" > "$2"
for f in $files; do
printf "\n/* ${f} */\n" >> "$2"
cat "$f" >> "$2"
done
}
ROOT_REPO_DIR="$(git rev-parse --show-toplevel)"
cd "${ROOT_REPO_DIR}"/src
collectCSSForTwine "${ROOT_REPO_DIR}" "${ROOT_REPO_DIR}/devNotes/twine CSS.txt"
@echo off
:: Generates devNotes/twineJS.txt from all .js files in src/ subdir
:: See if we can find a git installation
setlocal enabledelayedexpansion
for %%k in (HKCU HKLM) do (
for %%w in (\ \Wow6432Node\) do (
for /f "skip=2 delims=: tokens=1*" %%a in ('reg query "%%k\SOFTWARE%%wMicrosoft\Windows\CurrentVersion\Uninstall\Git_is1" /v InstallLocation 2^> nul') do (
for /f "tokens=3" %%z in ("%%a") do (
set GIT=%%z:%%b
set GITFOUND=yes
goto FOUND
)
)
)
)
:FOUND
if %GITFOUND% == yes (
set "PATH=%GIT%bin;%PATH%"
bash --login -c ./makeTwineJSPassage.sh
)
#!/bin/sh
# Generates devNotes/twine JS.txt from all .js files in src/ subdir
# Joins all .js files from the current dir (recursive) into a Twee [script] passage
# arguments:
# $1: root repo dir
# $2: output file name
collectJSForTwine() {
local files=$(find js src -path ./src/art/assistantArt.js -prune -o -name '*.js' -print)
files=$(echo "$files" | sort)
echo "" > "$2"
for f in $files; do
printf "\n/* ${f} */\n" >> "$2"
sed -nf "$1"/devTools/stripComments.sed "$f" >> "$2"
done
}
ROOT_REPO_DIR="$(git rev-parse --show-toplevel)"
cd "${ROOT_REPO_DIR}"
collectJSForTwine "${ROOT_REPO_DIR}" "${ROOT_REPO_DIR}/devNotes/twine JS.txt"
#! /bin/sed -nf
# Remove C and C++ comments, by Brian Hiles (brian_hiles.com)
# taken from https://bash.cyberciti.biz/academic/sed-remove-c-cpp-comments/
# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini.org)
# Works its way through the line, copying to hold space the text up to the
# first special character (/, ", '). The original version went exactly a
# character at a time, hence the greater speed of this one. But the concept
# and especially the trick of building the line in hold space are entirely
# merit of Brian.
:loop
# ezsh: commented out because it removes '//' and everything after that in string literals
# This line is sufficient to remove C++ comments!
#/^\/\// s,.*,,
/^$/{
x
p
n
b loop
}
/^"/{
:double
/^$/{
x
p
n
/^"/b break
b double
}
H
x
s,\n\(.[^\"]*\).*,\1,
x
s,.[^\"]*,,
/^"/b break
/^\\/{
H
x
s,\n\(.\).*,\1,
x
s/.//
}
b double
}
/^'/{
:single
/^$/{
x
p
n
/^'/b break
b single
}
H
x
s,\n\(.[^\']*\).*,\1,
x
s,.[^\']*,,
/^'/b break
/^\\/{
H
x
s,\n\(.\).*,\1,
x
s/.//
}
b single
}
/^\/\*/{
s/.//
:ccom
s,^.[^*]*,,
/^$/ n
/^\*\//{
s/..//
b loop
}
b ccom
}
:break
H
x
s,\n\(.[^"'/]*\).*,\1,
x
s/.[^"'/]*//
b loop
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="FinalHTML" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="devTools\FC.targets" />
<PropertyGroup>
<OutputDirectory>.\bin\</OutputDirectory>
<JSMoudleFileName>fc.js</JSMoudleFileName>
</PropertyGroup>
<PropertyGroup Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)' == 'X64' ">
<ArchitectureSuffix>64</ArchitectureSuffix>
</PropertyGroup>
<PropertyGroup Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)' == 'X86' ">
<ArchitectureSuffix>86</ArchitectureSuffix>
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(Windows))' ">
<TweeGoExe>tweego_win$(ArchitectureSuffix).exe</TweeGoExe>
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(Linux))' ">
<TweeGoExe>tweego_nix$(ArchitectureSuffix)</TweeGoExe>
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(OSX))' ">
<TweeGoExe>tweego_osx$(ArchitectureSuffix)</TweeGoExe>
</PropertyGroup>
<Target Name="CollectGitInfo" DependsOnTargets="FindGit">
<Exec Command="$(GitExe) rev-parse --short HEAD" ConsoleToMsBuild="true" EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitHash" />
</Exec>
</Target>
<Target Name="DisplayMessages" DependsOnTargets="CollectGitInfo">
<Message Text="MSBuildProjectDirectory: $(MSBuildProjectDirectory)" />
<Message Text="ArchitectureSuffix: $(ArchitectureSuffix)" Importance="high" />
<Message Text="TweeGoExe: $(TweeGoExe)" Importance="high" />
<Message Text="PA: $([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)" />
<Message Text="Git found: $(GitExeFound)" Importance="high" />
<Message Text="Git executable: $(GitExe)" Importance="high" />
<Message Text="HEAD commit hash: $(GitHash)" Importance="high" />
</Target>
<Target Name="createDirs">
<MakeDir Directories="$(OutputDirectory)\resources"/>
</Target>
<Target Name="JSModule" DependsOnTargets="createDirs;DisplayMessages">
<ItemGroup>
<jsScripts Include=".\js\**\*.js" /> <!-- will be sorted aphabetically -->
</ItemGroup>
<ConcatFiles Inputs="@(jsSCripts)" BaseDir="$(MSBuildProjectDirectory)" Output="$(OutputDirectory)\$(JSMoudleFileName)"/>
</Target>
<Target Name="InjectGitHash" DependsOnTargets="CollectGitInfo" Condition=" '$(GitExeFound)'" >
<WriteLinesToFile
File="src/002-config/fc-version.js.commitHash.js"
Lines="App.Version.commitHash = '$(GitHash)'%3B"
Overwrite="true"
Encoding="UTF-8"/>
</Target>
<Target Name="RemoveGeneratedFiles" DependsOnTargets="FindGit" Condition=" '$(GitExeFound)'" >
<Delete Files="src/002-config/fc-version.js.commitHash.js"/>
</Target>
<Target Name="tmpOutput" DependsOnTargets="JSModule;InjectGitHash">
<Exec Command="devTools\tweeGo\$(TweeGoExe) --module=$(OutputDirectory)\$(JSMoudleFileName) --head devTools\head.html -o $(OutputDirectory)\tmp.html src\" />
</Target>
<Target Name="FinalHTML" DependsOnTargets="tmpOutput;RemoveGeneratedFiles">
<Delete Files="$(OutputDirectory)\$(JSMoudleFileName)" />
<Move SourceFiles="$(OutputDirectory)\tmp.html" DestinationFiles="$(OutputDirectory)\FC_pregmod.html" />
</Target>
<Target Name="JavaSanityCheck">
<Exec Command="java -jar .\devTools\javaSanityCheck\SanityCheck.jar" />
</Target>
<!-- Twine targets -->
<Target Name="TwineCSS">
<ItemGroup>
<cssFiles Include=".\src\**\*.css" /> <!-- will be sorted aphabetically -->
</ItemGroup>
<ConcatFiles Inputs="@(cssFiles)" BaseDir="$(MSBuildProjectDirectory)" Output="$(MSBuildProjectDirectory)\devNotes\twine CSS.txt"/>
</Target>
<Target Name="TwineJS">
<ItemGroup>
<jsFiles Include=".\js\**\*.js;.\src\**\*.js" Exclude=".\src\art\assistantArt.js"/> <!-- will be sorted aphabetically -->
</ItemGroup>
<ConcatFiles Inputs="@(jsFiles)" BaseDir="$(MSBuildProjectDirectory)" Output="$(MSBuildProjectDirectory)\devNotes\twine JS.raw"/>
<StripComments
InputFilename="$(MSBuildProjectDirectory)\devNotes\twine JS.raw"
OutputFilename="$(MSBuildProjectDirectory)\devNotes\twine JS.txt"
/>
</Target>
<Target Name="Twine" DependsOnTargets="TwineCSS;TwineJS"/>
</Project>
...@@ -7,6 +7,7 @@ const gulp = require('gulp'), ...@@ -7,6 +7,7 @@ const gulp = require('gulp'),
shell = require('gulp-shell'), shell = require('gulp-shell'),
sort = require('gulp-sort'), sort = require('gulp-sort'),
sourcemaps = require('gulp-sourcemaps'), sourcemaps = require('gulp-sourcemaps'),
stripCssJSComments = require('gulp-strip-comments'),
autoprefixer = require('autoprefixer'), autoprefixer = require('autoprefixer'),
which = require('which'), which = require('which'),
fs = require('fs'), fs = require('fs'),
...@@ -173,10 +174,7 @@ function prepareComponent(name) { ...@@ -173,10 +174,7 @@ function prepareComponent(name) {
function makeThemeCompilationTask(themeName) { function makeThemeCompilationTask(themeName) {
const taskName = `make-theme-${themeName}`; const taskName = `make-theme-${themeName}`;
gulp.task(taskName, function() { gulp.task(taskName, function() {
return gulp.src(`themes/${themeName}/**/*.css`) return concatFiles(`themes/${themeName}/**/*.css`, cfg.dirs.output, `${themeName}.css`);
.pipe(sort())
.pipe(concat(`${themeName}.css`))
.pipe(gulp.dest(cfg.dirs.output));
}); });
return taskName; return taskName;
} }
...@@ -203,12 +201,11 @@ if (fs.statSync('.git').isDirectory()) { ...@@ -203,12 +201,11 @@ if (fs.statSync('.git').isDirectory()) {
gulp.task('buildHTML', gulp.series(prepare, 'compileStory')); gulp.task('buildHTML', gulp.series(prepare, 'compileStory'));
} }
const themeTasks = []; const themeTasks = fs.readdirSync('themes')
for (const entry of fs.readdirSync('themes')) { .filter(entry => fs.statSync(path.join('themes', entry)).isDirectory())
if (fs.statSync(path.join('themes', entry)).isDirectory()) { .map(entry => makeThemeCompilationTask(entry));
themeTasks.push(makeThemeCompilationTask(entry));
}
}
exports.html = gulp.series('buildHTML', moveHTMLInPlace); exports.html = gulp.series('buildHTML', moveHTMLInPlace);
exports.themes = gulp.parallel(themeTasks); exports.themes = gulp.parallel(themeTasks);
...@@ -216,3 +213,19 @@ exports.clean = clean; ...@@ -216,3 +213,19 @@ exports.clean = clean;
exports.default = exports.html; exports.default = exports.html;
exports.all = gulp.parallel(exports.html, exports.themes); exports.all = gulp.parallel(exports.html, exports.themes);
// legacy tasks
gulp.task('twineCSS', function() {
return concatFiles([...cfg.sources.module.css, ...cfg.sources.story.css], 'devNotes', 'twine CSS.txt');
});
gulp.task('twineJS', function() {
return gulp.src([...cfg.sources.module.js, ...cfg.sources.story.js, '!src/art/assistantArt.js'])
.pipe(stripCssJSComments({trim: true}))
.pipe(sort())
.pipe(concat('twine JS.txt'))
.pipe(gulp.dest('devNotes'));
});
exports.twine = gulp.parallel('twineCSS', 'twineJS');
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
"eslint": "^7.6.0", "eslint": "^7.6.0",
"eslint-plugin-jsdoc": "^32.0.0", "eslint-plugin-jsdoc": "^32.0.0",
"eslint-plugin-sonarjs": "^0.6.0", "eslint-plugin-sonarjs": "^0.6.0",
"ts-essentials": "^7.0.0",
"typescript": "^4.2.0"
},
"dependencies": {
"fancy-log-levels": "^1.0.0", "fancy-log-levels": "^1.0.0",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"gulp-concat": "^2.6.1", "gulp-concat": "^2.6.1",
...@@ -30,8 +34,7 @@ ...@@ -30,8 +34,7 @@
"gulp-shell": "^0.8.0", "gulp-shell": "^0.8.0",
"gulp-sort": "^2.0.0", "gulp-sort": "^2.0.0",
"gulp-sourcemaps": "^3.0.0", "gulp-sourcemaps": "^3.0.0",
"ts-essentials": "^7.0.0", "gulp-strip-comments": "^2.5.2",
"typescript": "^4.2.0",
"which": "^2.0.2", "which": "^2.0.2",
"yargs": "^16.0.0" "yargs": "^16.0.0"
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment