Skip to content

Commit

Permalink
Merge pull request #1012 from WolframResearch/1009-secondary-resource…
Browse files Browse the repository at this point in the history
…s-are-not-inlined-into-the-mx-build

Bugfix: Inline secondary resources into the MX build
  • Loading branch information
rhennigan authored Jan 6, 2025
2 parents 458038d + d505129 commit c6acff4
Showing 1 changed file with 66 additions and 5 deletions.
71 changes: 66 additions & 5 deletions Source/Chatbook/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ $resourceVersions = <|
"MessageFailure" -> "1.0.0",
"RelativeTimeString" -> "1.0.0",
"ReplaceContext" -> "1.0.0",
"ResourceFunctionMessage" -> "2.1.1",
"SecondsToQuantity" -> "1.2.0",
"SelectByCurrentValue" -> "1.0.1"
|>;

Expand Down Expand Up @@ -471,13 +473,17 @@ importResourceFunction // beginDefinition;
importResourceFunction::failure = "[ERROR] Failed to import resource function `1`. Aborting MX build.";
importResourceFunction // Attributes = { HoldFirst };

importResourceFunction[ name_String ] :=
importResourceFunction[ None, name ];

importResourceFunction[ symbol_Symbol, name_String ] :=
importResourceFunction[ symbol, name, Lookup[ $resourceVersions, name ] ];

importResourceFunction[ symbol_Symbol, name_String, version_String ] /; $mxFlag := Enclose[
importResourceFunction[ symbol_Symbol, name_String, version_ ] /; $mxFlag := Enclose[
Block[ { PrintTemporary },
Module[ { sourceContext, targetContext, definition, replaced, newSymbol },
Module[ { sourceContext, targetContext, definition, replaced, inlined, newSymbol },

ConfirmAssert @ StringQ @ version;
sourceContext = ConfirmBy[ ResourceFunction[ name, "Context", ResourceVersion -> version ], StringQ ];
targetContext = $resourceFunctionContext<>name<>"`";
definition = ConfirmMatch[ ResourceFunction[ name, "DefinitionList" ], _Language`DefinitionList ];
Expand All @@ -490,21 +496,75 @@ importResourceFunction[ symbol_Symbol, name_String, version_String ] /; $mxFlag
_Language`DefinitionList
];

ConfirmMatch[ Language`ExtendedFullDefinition[ ] = replaced, _Language`DefinitionList ];
inlined = ConfirmMatch[ inlineDependentResourceFunctions @ replaced, _Language`DefinitionList ];

$importedResourceFunctions[ name ] = version;
KeyDropFrom[ $dependentResourceFunctions, Keys @ $importedResourceFunctions ];

ConfirmMatch[ Language`ExtendedFullDefinition[ ] = inlined, _Language`DefinitionList ];

newSymbol = ConfirmMatch[ Symbol[ targetContext<>name ], _Symbol? AtomQ ];

ConfirmMatch[ symbol = newSymbol, newSymbol ]
importResourceFunction[ symbol, name, version ] =
If[ Unevaluated @ symbol === None,
newSymbol,
ConfirmMatch[ symbol = newSymbol, newSymbol ]
]
]
],
(Message[ importResourceFunction::failure, name ]; Abort[ ]) &
];

importResourceFunction[ symbol_Symbol, name_String, version_String ] :=
importResourceFunction[ symbol: Except[ None, _Symbol ], name_String, version_String ] :=
symbol := symbol = Block[ { PrintTemporary }, ResourceFunction[ name, "Function", ResourceVersion -> version ] ];

importResourceFunction // endDefinition;

$importedResourceFunctions = <| |>;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*importDependentResourceFunctions*)
importDependentResourceFunctions // beginDefinition;

importDependentResourceFunctions[ ] :=
importDependentResourceFunctions @ Keys @ $dependentResourceFunctions;

importDependentResourceFunctions[ { } ] :=
Null;

importDependentResourceFunctions[ names: { __String } ] := (
importResourceFunction /@ names;
KeyDropFrom[ $dependentResourceFunctions, names ];
importDependentResourceFunctions @ Keys @ $dependentResourceFunctions
);

importDependentResourceFunctions // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*inlineDependentResourceFunctions*)
inlineDependentResourceFunctions // beginDefinition;

inlineDependentResourceFunctions[ definition_ ] := ReplaceAll[
definition,
{
HoldPattern @ ResourceFunction[ name_String, OptionsPattern[ ] ] :> RuleCondition[
$dependentResourceFunctions[ name ] = True;
Symbol[ $resourceFunctionContext<>name<>"`"<>name ]
],
HoldPattern @ ResourceFunction[ name_String, "Function", OptionsPattern[ ] ] :> RuleCondition[
$dependentResourceFunctions[ name ] = True;
Symbol[ $resourceFunctionContext<>name<>"`"<>name ]
]
}
];

inlineDependentResourceFunctions // endDefinition;


$dependentResourceFunctions = <| |>;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Error Handling*)
Expand Down Expand Up @@ -1509,6 +1569,7 @@ addToMXInitialization[
$cloudTextResources;
$inlineExpressionRules;
$releaseID;
importDependentResourceFunctions[ ];
];

(* :!CodeAnalysis::EndBlock:: *)
Expand Down

0 comments on commit c6acff4

Please sign in to comment.