Skip to content

Commit c6acff4

Browse files
authored
Merge pull request #1012 from WolframResearch/1009-secondary-resources-are-not-inlined-into-the-mx-build
Bugfix: Inline secondary resources into the MX build
2 parents 458038d + d505129 commit c6acff4

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

Source/Chatbook/Common.wl

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ $resourceVersions = <|
141141
"MessageFailure" -> "1.0.0",
142142
"RelativeTimeString" -> "1.0.0",
143143
"ReplaceContext" -> "1.0.0",
144+
"ResourceFunctionMessage" -> "2.1.1",
145+
"SecondsToQuantity" -> "1.2.0",
144146
"SelectByCurrentValue" -> "1.0.1"
145147
|>;
146148

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

476+
importResourceFunction[ name_String ] :=
477+
importResourceFunction[ None, name ];
478+
474479
importResourceFunction[ symbol_Symbol, name_String ] :=
475480
importResourceFunction[ symbol, name, Lookup[ $resourceVersions, name ] ];
476481

477-
importResourceFunction[ symbol_Symbol, name_String, version_String ] /; $mxFlag := Enclose[
482+
importResourceFunction[ symbol_Symbol, name_String, version_ ] /; $mxFlag := Enclose[
478483
Block[ { PrintTemporary },
479-
Module[ { sourceContext, targetContext, definition, replaced, newSymbol },
484+
Module[ { sourceContext, targetContext, definition, replaced, inlined, newSymbol },
480485

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

493-
ConfirmMatch[ Language`ExtendedFullDefinition[ ] = replaced, _Language`DefinitionList ];
499+
inlined = ConfirmMatch[ inlineDependentResourceFunctions @ replaced, _Language`DefinitionList ];
500+
501+
$importedResourceFunctions[ name ] = version;
502+
KeyDropFrom[ $dependentResourceFunctions, Keys @ $importedResourceFunctions ];
503+
504+
ConfirmMatch[ Language`ExtendedFullDefinition[ ] = inlined, _Language`DefinitionList ];
494505

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

497-
ConfirmMatch[ symbol = newSymbol, newSymbol ]
508+
importResourceFunction[ symbol, name, version ] =
509+
If[ Unevaluated @ symbol === None,
510+
newSymbol,
511+
ConfirmMatch[ symbol = newSymbol, newSymbol ]
512+
]
498513
]
499514
],
500515
(Message[ importResourceFunction::failure, name ]; Abort[ ]) &
501516
];
502517

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

506521
importResourceFunction // endDefinition;
507522

523+
$importedResourceFunctions = <| |>;
524+
525+
(* ::**************************************************************************************************************:: *)
526+
(* ::Subsection::Closed:: *)
527+
(*importDependentResourceFunctions*)
528+
importDependentResourceFunctions // beginDefinition;
529+
530+
importDependentResourceFunctions[ ] :=
531+
importDependentResourceFunctions @ Keys @ $dependentResourceFunctions;
532+
533+
importDependentResourceFunctions[ { } ] :=
534+
Null;
535+
536+
importDependentResourceFunctions[ names: { __String } ] := (
537+
importResourceFunction /@ names;
538+
KeyDropFrom[ $dependentResourceFunctions, names ];
539+
importDependentResourceFunctions @ Keys @ $dependentResourceFunctions
540+
);
541+
542+
importDependentResourceFunctions // endDefinition;
543+
544+
(* ::**************************************************************************************************************:: *)
545+
(* ::Subsection::Closed:: *)
546+
(*inlineDependentResourceFunctions*)
547+
inlineDependentResourceFunctions // beginDefinition;
548+
549+
inlineDependentResourceFunctions[ definition_ ] := ReplaceAll[
550+
definition,
551+
{
552+
HoldPattern @ ResourceFunction[ name_String, OptionsPattern[ ] ] :> RuleCondition[
553+
$dependentResourceFunctions[ name ] = True;
554+
Symbol[ $resourceFunctionContext<>name<>"`"<>name ]
555+
],
556+
HoldPattern @ ResourceFunction[ name_String, "Function", OptionsPattern[ ] ] :> RuleCondition[
557+
$dependentResourceFunctions[ name ] = True;
558+
Symbol[ $resourceFunctionContext<>name<>"`"<>name ]
559+
]
560+
}
561+
];
562+
563+
inlineDependentResourceFunctions // endDefinition;
564+
565+
566+
$dependentResourceFunctions = <| |>;
567+
508568
(* ::**************************************************************************************************************:: *)
509569
(* ::Section::Closed:: *)
510570
(*Error Handling*)
@@ -1509,6 +1569,7 @@ addToMXInitialization[
15091569
$cloudTextResources;
15101570
$inlineExpressionRules;
15111571
$releaseID;
1572+
importDependentResourceFunctions[ ];
15121573
];
15131574

15141575
(* :!CodeAnalysis::EndBlock:: *)

0 commit comments

Comments
 (0)