Skip to content

Commit

Permalink
Avoid some repetition to speed up automatic builds
Browse files Browse the repository at this point in the history
  • Loading branch information
rhennigan committed Dec 31, 2024
1 parent 67273d4 commit 488b666
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 140 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,23 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Check
run: wolframscript -f Scripts/CheckPaclet.wls

- name: Build
run: wolframscript -f Scripts/BuildPaclet.wls

- name: UploadArtifact
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
path: ${{ env.PACLET_BUILD_DIR }}

- name: InstallTestDependencies
- name: Install Test Dependencies
run: |
apt-get update && apt-get install libgomp1 -y
wolframscript -f Scripts/InstallTestDependencies.wls
- name: Test
run: wolframscript -f Scripts/TestPaclet.wls

- name: UploadStackData
- name: Upload Stack Data
if: always() && env.PACLET_STACK_HISTORY
uses: actions/upload-artifact@v4
with:
Expand Down
8 changes: 6 additions & 2 deletions Scripts/BuildMX.wls
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Needs[ "PacletTools`" -> "pt`" ];
Needs[ "Wolfram`PacletCICD`" -> "cicd`" ];

Wolfram`ChatbookInternal`$BuildingMX = True;
$assertions = getBooleanArgument[ { "a", "assertions" }, True ];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
Expand Down Expand Up @@ -152,8 +153,11 @@ If[ FileExistsQ @ $mxFile,
cicd`ConsoleLog[ "Copying files..." ];
tmp = cicd`ScriptConfirmBy[ copyTemporary @ $pacletDir, DirectoryQ ];
cicd`ScriptConfirmBy[ setPacletReleaseID[ tmp, releaseID @ $pacletDir ], StringQ ];
cicd`ConsoleLog[ "Inserting confirmation source info..." ];
cicd`ScriptConfirm @ expandTags @ tmp;

If[ $assertions,
cicd`ConsoleLog[ "Inserting confirmation source info..." ];
cicd`ScriptConfirm @ expandTags @ tmp
];

cicd`ConsoleLog[ "Loading paclet..." ];
PacletDirectoryUnload @ $pacletDir;
Expand Down
25 changes: 21 additions & 4 deletions Scripts/BuildPaclet.wls
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@ BeginPackage[ "Wolfram`ChatbookScripts`" ];
(* ::Section::Closed:: *)
(*Initialization*)
If[ ! TrueQ @ $loadedDefinitions, Get @ FileNameJoin @ { DirectoryName @ $InputFileName, "Common.wl" } ];
Get @ cFile @ FileNameJoin @ { DirectoryName @ $InputFileName, "UnformatFiles.wls" };
Get @ cFile @ FileNameJoin @ { DirectoryName @ $InputFileName, "BuildMX.wls" };

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*Arguments*)
$check = getBooleanArgument[ { "c", "check" }, True ];
$install = getBooleanArgument[ { "i", "install" }, False ];
$mx = getBooleanArgument[ { "m", "mx" }, True ];
$unformat = getBooleanArgument[ { "u", "unformat" }, True ];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*Optional Dependencies*)
If[ $unformat, Get @ cFile @ FileNameJoin @ { $scriptDir, "UnformatFiles.wls" } ];
If[ $mx , Get @ cFile @ FileNameJoin @ { $scriptDir, "BuildMX.wls" } ];
If[ $check , Get @ cFile @ FileNameJoin @ { $scriptDir, "Resources", "CodeInspectorRules.wl" } ];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*Other*)
Needs[ "Wolfram`PacletCICD`" -> "cicd`" ];

(* ::**************************************************************************************************************:: *)
Expand All @@ -19,15 +36,15 @@ Needs[ "Wolfram`PacletCICD`" -> "cicd`" ];
(*Build*)
result = checkResult @ cicd`BuildPaclet[
$defNB,
"Check" -> False,
"Check" -> $check,
"ExitOnFail" -> True,
"Target" -> "Submit"
];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*Install*)
If[ MemberQ[ $scriptCommandLine, "-i"|"--install"|"--install=true" ],
If[ $install,
archive = cFile @ result[ "PacletArchive" ];
cicd`ConsoleNotice @ SequenceForm[ "Installing paclet file: ", archive ];
installed = cicd`ScriptConfirmBy[ PacletInstall[ archive, ForceVersionInstall -> True ], PacletObjectQ ];
Expand Down
124 changes: 1 addition & 123 deletions Scripts/CheckPaclet.wls
Original file line number Diff line number Diff line change
Expand Up @@ -3,129 +3,7 @@
BeginPackage[ "Wolfram`ChatbookScripts`" ];

If[ ! TrueQ @ $loadedDefinitions, Get @ FileNameJoin @ { DirectoryName @ $InputFileName, "Common.wl" } ];

Needs[ "CodeInspector`" -> "ci`" ];
Needs[ "CodeParser`" -> "cp`" ];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Config*)
$inGitHub := $inGitHub = StringQ @ Environment[ "GITHUB_ACTIONS" ];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Custom Rules*)
CodeInspector`AbstractRules`$DefaultAbstractRules = <|
CodeInspector`AbstractRules`$DefaultAbstractRules,
cp`CallNode[ cp`LeafNode[ Symbol, "Throw", _ ], { _ }, _ ] -> scanSingleArgThrow,
cp`LeafNode[ Symbol, _String? privateContextQ, _ ] -> scanPrivateContext,
cp`LeafNode[ Symbol, _String? globalSymbolQ, _ ] -> scanGlobalSymbol
|>;

CodeInspector`ConcreteRules`$DefaultConcreteRules = <|
CodeInspector`ConcreteRules`$DefaultConcreteRules,
cp`LeafNode[
Token`Comment,
_String? (StringStartsQ[ "(*"~~WhitespaceCharacter...~~"FIXME:" ]),
_
] -> scanFixMeComment
|>;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*scanSingleArgThrow*)
scanSingleArgThrow // ClearAll;
scanSingleArgThrow[ pos_, ast_ ] := Catch[
Replace[
Fold[ walkASTForCatch, ast, pos ],
{
cp`CallNode[ cp`LeafNode[ Symbol, "Throw", _ ], _, as_Association ] :>
ci`InspectionObject[
"NoSurroundingCatch",
"``Throw`` has no tag or surrounding ``Catch``",
"Error",
<| as, ConfidenceLevel -> 0.9 |>
],
___ :> { }
}
],
$tag
];

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*walkASTForCatch*)
walkASTForCatch // ClearAll;

walkASTForCatch[ cp`CallNode[ cp`LeafNode[ Symbol, "Catch"|"Hold"|"HoldForm"|"HoldComplete", _ ], { _ }, _ ], _ ] :=
Throw[ { }, $tag ];

walkASTForCatch[ ast_, pos_ ] :=
Extract[ ast, pos ];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*scanPrivateContext*)
scanPrivateContext // ClearAll;
scanPrivateContext[ pos_, ast_ ] :=
Enclose @ Module[ { node, name, as },
node = ConfirmMatch[ Extract[ ast, pos ], _[ _, _, __ ], "Node" ];
name = ConfirmBy[ node[[ 2 ]], StringQ, "Name" ];
as = ConfirmBy[ node[[ 3 ]], AssociationQ, "Metadata" ];
ci`InspectionObject[
"PrivateContextSymbol",
"The symbol ``" <> name <> "`` is in a private context",
"Warning",
<| as, ConfidenceLevel -> 0.9 |>
]
];

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*privateContextQ*)
privateContextQ // ClearAll;
privateContextQ[ name_String ] /; StringStartsQ[ name, "System`Private`" ] := False;
privateContextQ[ name_String ] := StringContainsQ[ name, __ ~~ ("`Private`"|"`PackagePrivate`") ];
privateContextQ[ ___ ] := False;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*scanGlobalSymbol*)
scanGlobalSymbol // ClearAll;
scanGlobalSymbol[ pos_, ast_ ] :=
Enclose @ Module[ { node, name, as },
node = ConfirmMatch[ Extract[ ast, pos ], _[ _, _, __ ], "Node" ];
name = ConfirmBy[ node[[ 2 ]], StringQ, "Name" ];
as = ConfirmBy[ node[[ 3 ]], AssociationQ, "Metadata" ];
ci`InspectionObject[
"GlobalSymbol",
"The symbol ``" <> name <> "`` is in the global context",
"Error",
<| as, ConfidenceLevel -> 0.9 |>
]
];

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*globalSymbolQ*)
globalSymbolQ // ClearAll;
globalSymbolQ[ name_String ] := StringStartsQ[ name, "Global`" ];
globalSymbolQ[ ___ ] := False;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*scanFixMeComment*)
scanFixMeComment // ClearAll;

scanFixMeComment[ pos_, ast_ ] /; $inGitHub :=
Enclose @ Module[ { node, comment, as },
node = ConfirmMatch[ Extract[ ast, pos ], _[ _, _, __ ], "Node" ];
comment = StringTrim @ StringTrim[ ConfirmBy[ node[[ 2 ]], StringQ, "Comment" ], { "(*", "*)" } ];
as = ConfirmBy[ node[[ 3 ]], AssociationQ, "Metadata" ];
ci`InspectionObject[ "FixMeComment", comment, "Remark", <| as, ConfidenceLevel -> 0.9 |> ]
];

scanFixMeComment[ pos_, ast_ ] := { };
Get @ cFile @ FileNameJoin @ { $scriptDir, "Resources", "CodeInspectorRules.wl" };

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
Expand Down
55 changes: 53 additions & 2 deletions Scripts/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ $messageHistory = <| |>;
$stackHistory = <| |>;
$inputFileName = cFile @ Replace[ $InputFileName, "" :> NotebookFileName[ ] ];
$pacletDir = cDir @ DirectoryName[ $inputFileName, 2 ];
$scriptDir = DirectoryName @ $inputFileName;

Internal`AddHandler[ "Message", messageHandler ];

Expand Down Expand Up @@ -105,9 +106,59 @@ messageString[ ___ ] := "-- Message text not found --";

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Definitions*)
$scriptCommandLine := Replace[ $ScriptCommandLine, { } :> $CommandLine ];
(*Command Line Arguments*)
$scriptCommandLine := Select[ Flatten @ { Replace[ $ScriptCommandLine, { } :> $CommandLine ] }, StringQ ];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*getBooleanArgument*)
getBooleanArgument // Attributes = { HoldRest };

getBooleanArgument[ name_ ] :=
getBooleanArgument[ name, False ];

getBooleanArgument[ name_String, default_ ] :=
getBooleanArgument[ { name, name }, default ];

getBooleanArgument[ { short_String, full_String }, default_ ] := Catch[
Module[ { named, interpreted, res },
If[ MemberQ[ $scriptCommandLine, "-"<>short | "--"<>full ], Throw[ True, $booleanTag ] ];
named = getNamedArgument @ full;
If[ ! StringQ @ named, Throw[ default, $booleanTag ] ];
interpreted = Interpreter[ "Boolean" ][ named ];
If[ BooleanQ @ interpreted,
interpreted,
res = default;
cicd`ConsoleError @ TemplateApply[
"The value \"`1`\" specified for \"`2`\" is not a valid boolean value. Using default value: \"`3`\".",
{ named, full, res }
];
res
]
],
$booleanTag
];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*getNamedArgument*)
getNamedArgument // Attributes = { HoldRest };

getNamedArgument[ name_ ] :=
getNamedArgument[ name, Missing[ "NotSpecified" ] ];

getNamedArgument[ name_String, default_ ] :=
Module[ { arg },
arg = SelectFirst[ $scriptCommandLine, StringQ @ # && StringStartsQ[ #, "--"<>name<>"=" ] & ];
If[ StringQ @ arg,
StringDelete[ arg, StartOfString ~~ "--"<>name<>"=" ],
default
]
];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Definitions*)
$$ws = WhitespaceCharacter...;
$$id = "\"" ~~ Except[ "\"" ].. ~~ "\"";

Expand Down
Loading

0 comments on commit 488b666

Please sign in to comment.