2121import org .kohsuke .stapler .QueryParameter ;
2222
2323import javax .annotation .Nonnull ;
24+ import java .io .IOException ;
2425import java .io .PrintStream ;
2526import java .util .Map ;
2627import java .util .ArrayList ;
@@ -69,49 +70,72 @@ public void perform(
6970 try {
7071 EnvVars envVars = TaskScopedEnvVarsManager .addRequiredEnvironmentVariables (run , env , listener );
7172
72- cliConfiguration .updateSelectedCliVersionKey (run , cliVersion );
73- boolean isSelectedCliAlreadyCached = cliConfiguration .getCliPath (launcher , envVars , cliVersion ).isPresent ();
73+ String scopedVersion = cliVersion ;
74+ UiPathCliConfiguration .Configuration versionConfiguration = null ;
75+ FilePath actualCliNupkgPath = null ;
7476
75- logger .println (isSelectedCliAlreadyCached ? "cli is already cached.." : "cli is not found in cache.." );
77+ if (scopedVersion .contains ("CustomVersion" )) {
78+ if (StringUtils .isBlank (cliNupkgPath )){
79+ throw new AbortException ("CustomVersion is selected, but path to local nupkg is not provided." );
80+ }
81+ try {
82+ actualCliNupkgPath = cliNupkgPath .contains ("${WORKSPACE}" ) ?
83+ new FilePath (launcher .getChannel (), envVars .expand (cliNupkgPath )) :
84+ workspace .child (envVars .expand (cliNupkgPath ));
7685
77- if (this .forceInstall || !isSelectedCliAlreadyCached ) {
86+ versionConfiguration = cliConfiguration .getConfigurationFromPackageName (actualCliNupkgPath .getBaseName ());
87+ scopedVersion = versionConfiguration .getConfigurationUniqueId ();
88+ cliConfiguration .AddCliConfiguration (scopedVersion , versionConfiguration );
89+ } catch (Exception e ) {
90+ logger .println ("Exception: " + e .getMessage ());
91+ throw new AbortException ("Failed to parse custom CLI version from path: " + cliNupkgPath + ". Make sure you didn't change default nupkg name downloaded from feed" );
92+ }
93+ }
94+ else {
95+ versionConfiguration = cliConfiguration .getConfiguration (scopedVersion );
96+ }
97+
98+ logger .println ("Validating CLI selected version: " + versionConfiguration .getDisplayName () + "..." );
99+ validateCliCompatibility (versionConfiguration , logger , workspace );
78100
101+ boolean isSelectedCliAlreadyCached = cliConfiguration .getCliPath (launcher , envVars , scopedVersion ).isPresent ();
102+
103+ logger .println (isSelectedCliAlreadyCached ? "cli " + scopedVersion + " is already cached.." : "cli " + scopedVersion + " is not found in cache.." );
104+
105+ if (this .forceInstall || !isSelectedCliAlreadyCached ) {
79106 if (forceInstall ) {
80- logger .println ("force installing the cli , any previous cache for version " + cliVersion + " will be invalidate .." );
107+ logger .println ("force installing the cli , any previous cache for version " + scopedVersion + " will be invalidated .." );
81108 }
82109
83- FilePath cliRootCacheDirPath = cliConfiguration .getCliRootCachedDirectoryPath (launcher , envVars , cliVersion );
110+ FilePath cliRootCacheDirPath = cliConfiguration .getCliRootCachedDirectoryPath (launcher , envVars , scopedVersion );
84111
85- if (cliVersion .equals (cliConfiguration .getDefaultCliVersionKey ())) {
112+ if (scopedVersion .equals (cliConfiguration .getDefaultCliVersionKey ())) {
86113 logger .print ("(caching) extracting the pre-packaged cli..." );
87114 util .extractCliApp (cliRootCacheDirPath , listener , envVars );
88115
89- } else if (StringUtils .isNotBlank (cliNupkgPath )) {
90-
91- FilePath actualCliNupkgPath = cliNupkgPath .contains ("${WORKSPACE}" ) ?
92- new FilePath (launcher .getChannel (), envVars .expand (cliNupkgPath )) :
93- workspace .child (envVars .expand (cliNupkgPath ));
94-
116+ } else if (cliVersion .contains ("CustomVersion" ) && StringUtils .isNotBlank (cliNupkgPath )) {
95117 if (!actualCliNupkgPath .exists ()){
96118 logger .println ("CliNupkgPath provided doesn't exists " +actualCliNupkgPath .getRemote ());
97119 throw new AbortException (Messages .UiPathInstallPlatform_DescriptorImpl_Error_CliNupkgPath ());
98120 }
99- logger .print ("(caching) extracting the provided cli-nuget..." );
121+ logger .println ("(caching) extracting provided cli-nuget from path " + actualCliNupkgPath . getRemote () );
100122 actualCliNupkgPath .unzip (cliRootCacheDirPath );
101123 } else {
102- UiPathCliConfiguration .Configuration configuration = cliConfiguration .getConfiguration ().get (cliVersion );
103- FilePath downloadsRootPath = cliConfiguration .getCliRootDownloadsDirectoryPath (launcher , envVars , cliVersion );
124+ UiPathCliConfiguration .Configuration configuration = cliConfiguration .getConfiguration ().get (scopedVersion );
125+ FilePath downloadsRootPath = cliConfiguration .getCliRootDownloadsDirectoryPath (launcher , envVars , scopedVersion );
104126
105127 String fileName = configuration .getName ().concat ("." ).concat (configuration .getVersion ().getComplete ()).concat (".nupkg" );
106128
107129 FilePath downloadCliPath = downloadsRootPath .child (fileName );
108130 util .downloadCli (configuration .getFeedUrl (), downloadCliPath , listener );
109131
110- logger .print ("(caching) extracting the downloaded cli..." );
132+ logger .println ("(caching) extracting the downloaded cli..." );
111133 downloadCliPath .unzip (cliRootCacheDirPath );
112134 }
113- logger .println (" done!!" );
135+ logger .println ("Finished extraction for UipCLI version: " + scopedVersion );
114136 }
137+
138+ cliConfiguration .updateSelectedCliVersionKey (run , scopedVersion );
115139 } catch (Exception e ) {
116140 if (traceLevel .equals (TraceLevel .Verbose ) || traceLevel .equals (TraceLevel .Error )) {
117141 e .printStackTrace (logger );
@@ -120,6 +144,36 @@ public void perform(
120144 }
121145 }
122146
147+ private void validateCliCompatibility (UiPathCliConfiguration .Configuration cliSelectedVersion , PrintStream logger , FilePath workspace ) throws AbortException {
148+ Computer computer = workspace .toComputer ();
149+ if (computer == null ) {
150+ throw new AbortException ("Unable to determine the operating system of the agent." );
151+ }
152+
153+ String osName = null ;
154+ try {
155+ osName = computer .getSystemProperties ().get ("os.name" ).toString ().toLowerCase ();
156+ } catch (IOException e ) {
157+ throw new RuntimeException (e );
158+ } catch (InterruptedException e ) {
159+ throw new RuntimeException (e );
160+ }
161+
162+ logger .println ("Detected OS: " + osName );
163+
164+ if (osName .contains ("win" )) {
165+ if (cliSelectedVersion .getPlatform () != UiPathCliConfiguration .CliPlatform .Windows ) {
166+ throw new AbortException ("Selected UiPath CLI version '" + cliSelectedVersion .getDisplayName () + "' cannot be executed on Windows agent." );
167+ }
168+ } else if (osName .contains ("linux" )) {
169+ if (cliSelectedVersion .getPlatform () != UiPathCliConfiguration .CliPlatform .Linux ) {
170+ throw new AbortException ("Selected UiPath CLI version '" + cliSelectedVersion .getDisplayName () + "' cannot be executed on Linux agent." );
171+ }
172+ } else {
173+ throw new AbortException ("Running on incompatible operating system" );
174+ }
175+ }
176+
123177 @ DataBoundSetter
124178 public void setCliVersion (String cliVersion ) {
125179 this .cliVersion = cliVersion ;
0 commit comments