11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Reflection ;
5+ using mixpanel ;
46using UnityBuilderAction . Input ;
57using UnityBuilderAction . Reporting ;
68using UnityBuilderAction . Versioning ;
1012
1113namespace UnityBuilderAction
1214{
13- static class Builder
14- {
15- public static void BuildProject ( )
15+ internal static class Builder
1616 {
17- // Gather values from args
18- var options = ArgumentsParser . GetValidatedOptions ( ) ;
17+ public static void BuildProject ( )
18+ {
19+ // Gather values from args
20+ var options = ArgumentsParser . GetValidatedOptions ( ) ;
1921
20- // Gather values from project
21- var scenes = EditorBuildSettings . scenes . Where ( scene => scene . enabled ) . Select ( s => s . path ) . ToArray ( ) ;
22-
23- // Get all buildOptions from options
24- BuildOptions buildOptions = BuildOptions . None ;
25- foreach ( string buildOptionString in Enum . GetNames ( typeof ( BuildOptions ) ) ) {
26- if ( options . ContainsKey ( buildOptionString ) ) {
27- BuildOptions buildOptionEnum = ( BuildOptions ) Enum . Parse ( typeof ( BuildOptions ) , buildOptionString ) ;
28- buildOptions |= buildOptionEnum ;
29- }
30- }
22+ InjectSecrets ( options ) ;
23+
24+ // Gather values from project
25+ var scenes = EditorBuildSettings . scenes . Where ( scene => scene . enabled ) . Select ( s => s . path ) . ToArray ( ) ;
26+
27+ // Get all buildOptions from options
28+ var buildOptions = BuildOptions . None ;
29+ foreach ( var buildOptionString in Enum . GetNames ( typeof ( BuildOptions ) ) )
30+ {
31+ if ( options . ContainsKey ( buildOptionString ) )
32+ {
33+ var buildOptionEnum = ( BuildOptions ) Enum . Parse ( typeof ( BuildOptions ) , buildOptionString ) ;
34+ buildOptions |= buildOptionEnum ;
35+ }
36+ }
3137
3238#if UNITY_2021_2_OR_NEWER
33- // Determine subtarget
34- StandaloneBuildSubtarget buildSubtarget ;
35- if ( ! options . TryGetValue ( "standaloneBuildSubtarget" , out var subtargetValue ) || ! Enum . TryParse ( subtargetValue , out buildSubtarget ) ) {
36- buildSubtarget = default ;
37- }
39+ // Determine subtarget
40+ StandaloneBuildSubtarget buildSubtarget ;
41+ if ( ! options . TryGetValue ( "standaloneBuildSubtarget" , out var subtargetValue ) || ! Enum . TryParse ( subtargetValue , out buildSubtarget ) )
42+ {
43+ buildSubtarget = default ;
44+ }
3845#endif
3946
40- // Define BuildPlayer Options
41- var buildPlayerOptions = new BuildPlayerOptions {
42- scenes = scenes ,
43- locationPathName = options [ "customBuildPath" ] ,
44- target = ( BuildTarget ) Enum . Parse ( typeof ( BuildTarget ) , options [ "buildTarget" ] ) ,
45- options = buildOptions ,
47+ // Define BuildPlayer Options
48+ var buildPlayerOptions = new BuildPlayerOptions
49+ {
50+ scenes = scenes ,
51+ locationPathName = options [ "customBuildPath" ] ,
52+ target = ( BuildTarget ) Enum . Parse ( typeof ( BuildTarget ) , options [ "buildTarget" ] ) ,
53+ options = buildOptions ,
4654#if UNITY_2021_2_OR_NEWER
47- subtarget = ( int ) buildSubtarget
55+ subtarget = ( int ) buildSubtarget
4856#endif
49- } ;
57+ } ;
5058
51- // Set version for this build
52- VersionApplicator . SetVersion ( options [ "buildVersion" ] ) ;
53-
54- // Apply Android settings
55- if ( buildPlayerOptions . target == BuildTarget . Android )
56- {
57- VersionApplicator . SetAndroidVersionCode ( options [ "androidVersionCode" ] ) ;
58- AndroidSettings . Apply ( options ) ;
59- }
60-
61- // Execute default AddressableAsset content build, if the package is installed.
62- // Version defines would be the best solution here, but Unity 2018 doesn't support that,
63- // so we fall back to using reflection instead.
64- var addressableAssetSettingsType = Type . GetType (
65- "UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,Unity.Addressables.Editor" ) ;
66- if ( addressableAssetSettingsType != null )
67- {
68- // ReSharper disable once PossibleNullReferenceException, used from try-catch
69- try
70- {
71- addressableAssetSettingsType . GetMethod ( "CleanPlayerContent" , BindingFlags . Static | BindingFlags . Public )
72- . Invoke ( null , new object [ ] { null } ) ;
73- addressableAssetSettingsType . GetMethod ( "BuildPlayerContent" , new Type [ 0 ] ) . Invoke ( null , new object [ 0 ] ) ;
74- }
75- catch ( Exception e )
76- {
77- Debug . LogError ( $ "Failed to run default addressables build:\n { e } ") ;
78- }
79- }
59+ // Set version for this build
60+ VersionApplicator . SetVersion ( options [ "buildVersion" ] ) ;
8061
81- // Perform build
82- BuildReport buildReport = BuildPipeline . BuildPlayer ( buildPlayerOptions ) ;
62+ // Apply Android settings
63+ if ( buildPlayerOptions . target == BuildTarget . Android )
64+ {
65+ VersionApplicator . SetAndroidVersionCode ( options [ "androidVersionCode" ] ) ;
66+ AndroidSettings . Apply ( options ) ;
67+ }
8368
84- // Summary
85- BuildSummary summary = buildReport . summary ;
86- StdOutReporter . ReportSummary ( summary ) ;
69+ // Execute default AddressableAsset content build, if the package is installed.
70+ // Version defines would be the best solution here, but Unity 2018 doesn't support that,
71+ // so we fall back to using reflection instead.
72+ var addressableAssetSettingsType = Type . GetType (
73+ "UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,Unity.Addressables.Editor" ) ;
74+ if ( addressableAssetSettingsType != null )
75+ {
76+ // ReSharper disable once PossibleNullReferenceException, used from try-catch
77+ try
78+ {
79+ addressableAssetSettingsType . GetMethod ( "CleanPlayerContent" , BindingFlags . Static | BindingFlags . Public )
80+ . Invoke ( null , new object [ ]
81+ {
82+ null
83+ } ) ;
84+ addressableAssetSettingsType . GetMethod ( "BuildPlayerContent" , new Type [ 0 ] ) . Invoke ( null , new object [ 0 ] ) ;
85+ }
86+ catch ( Exception e )
87+ {
88+ Debug . LogError ( $ "Failed to run default addressables build:\n { e } ") ;
89+ }
90+ }
8791
88- // Result
89- BuildResult result = summary . result ;
90- StdOutReporter . ExitWithResult ( result ) ;
92+ // Perform build
93+ var buildReport = BuildPipeline . BuildPlayer ( buildPlayerOptions ) ;
94+
95+ // Summary
96+ var summary = buildReport . summary ;
97+ StdOutReporter . ReportSummary ( summary ) ;
98+
99+ // Result
100+ var result = summary . result ;
101+ StdOutReporter . ExitWithResult ( result ) ;
102+ }
103+
104+ private static void InjectSecrets ( Dictionary < string , string > options )
105+ {
106+ if ( options . TryGetValue ( "mixpanelToken" , out var mixpanelToken ) )
107+ {
108+ var mixpanelSettings = MixpanelSettings . Instance ;
109+ mixpanelSettings . RuntimeToken = mixpanelToken ;
110+ EditorUtility . SetDirty ( mixpanelSettings ) ;
111+ }
112+
113+ AssetDatabase . SaveAssets ( ) ;
114+ }
91115 }
92- }
93- }
116+ }
0 commit comments