Skip to content

Commit 9ec4193

Browse files
committed
Merge branch 'yabo/test-devtsp' into yabo/tsp-client
2 parents 5602159 + 2e4a42a commit 9ec4193

File tree

8 files changed

+58
-65
lines changed

8 files changed

+58
-65
lines changed

.azure-pipelines/azure-powershell-ci.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ jobs:
9797
- task: NodeTool@0
9898
displayName: Install Autorest
9999
inputs:
100-
versionSpec: '18.20.8'
100+
versionSpec: '20.19.6'
101101
command: custom
102102
verbose: false
103103
customCommand: install autorest@latest
@@ -107,11 +107,6 @@ jobs:
107107
targetType: inline
108108
script: "$env:NODE_OPTIONS=\"--max-old-space-size=65536\""
109109
pwsh: true
110-
- task: Npm@1
111-
displayName: Install tsp client
112-
inputs:
113-
command: custom
114-
customCommand: install -g @azure-tools/[email protected]
115110
- task: PowerShell@2
116111
displayName: 'Build'
117112
inputs:
@@ -212,7 +207,7 @@ jobs:
212207
- task: NodeTool@0
213208
displayName: Install autorest
214209
inputs:
215-
versionSpec: '18.20.8'
210+
versionSpec: '20.19.6'
216211
command: custom
217212
verbose: false
218213
customCommand: install autorest@latest

.azure-pipelines/powershell-core.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- task: NodeTool@0
4848
displayName: Install Autorest
4949
inputs:
50-
versionSpec: '18.20.8'
50+
versionSpec: '20.19.6'
5151
command: custom
5252
verbose: false
5353
customCommand: install autorest@latest
@@ -57,11 +57,6 @@ jobs:
5757
targetType: inline
5858
script: "$env:NODE_OPTIONS=\"--max-old-space-size=65536\""
5959
pwsh: true
60-
- task: Npm@1
61-
displayName: Install tsp client
62-
inputs:
63-
command: custom
64-
customCommand: install -g @azure-tools/[email protected]
6560
- task: PowerShell@2
6661
displayName: 'Build'
6762
inputs:
@@ -107,7 +102,7 @@ jobs:
107102
- task: NodeTool@0
108103
displayName: Install autorest
109104
inputs:
110-
versionSpec: '18.20.8'
105+
versionSpec: '20.19.6'
111106
command: custom
112107
verbose: false
113108
customCommand: install autorest@latest

.azure-pipelines/security-tools.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- task: NodeTool@0
2828
displayName: Install Autorest
2929
inputs:
30-
versionSpec: '18.20.7'
30+
versionSpec: '20.19.6'
3131
command: custom
3232
verbose: false
3333
customCommand: install autorest@latest
@@ -37,11 +37,6 @@ jobs:
3737
targetType: inline
3838
script: "$env:NODE_OPTIONS=\"--max-old-space-size=65536\""
3939
pwsh: true
40-
- task: Npm@1
41-
displayName: Install tsp client
42-
inputs:
43-
command: custom
44-
customCommand: install -g @azure-tools/[email protected]
4540
- task: PowerShell@2
4641
displayName: 'Build'
4742
inputs:

.azure-pipelines/util/analyze-steps.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ steps:
1111
- task: NodeTool@0
1212
displayName: Install autorest
1313
inputs:
14-
versionSpec: '18.20.7'
14+
versionSpec: '20.19.6'
1515
command: custom
1616
verbose: false
1717
customCommand: install autorest@latest
@@ -21,11 +21,6 @@ steps:
2121
targetType: inline
2222
script: "$env:NODE_OPTIONS=\"--max-old-space-size=65536\""
2323
pwsh: true
24-
- task: Npm@1
25-
displayName: Install tsp client
26-
inputs:
27-
command: custom
28-
customCommand: install -g @azure-tools/[email protected]
2924
- task: UseDotNet@2
3025
displayName: 'Use .NET SDK'
3126
inputs:

.azure-pipelines/windows-powershell.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- task: NodeTool@0
4444
displayName: Install Autorest
4545
inputs:
46-
versionSpec: '18.20.8'
46+
versionSpec: '20.19.6'
4747
command: custom
4848
verbose: false
4949
customCommand: install autorest@latest
@@ -53,11 +53,6 @@ jobs:
5353
targetType: inline
5454
script: "$env:NODE_OPTIONS=\"--max-old-space-size=65536\""
5555
pwsh: true
56-
- task: Npm@1
57-
displayName: Install tsp client
58-
inputs:
59-
command: custom
60-
customCommand: install -g @azure-tools/[email protected]
6156
- task: PowerShell@2
6257
displayName: 'Build'
6358
inputs:
@@ -103,7 +98,7 @@ jobs:
10398
- task: NodeTool@0
10499
displayName: Install autorest
105100
inputs:
106-
versionSpec: '18.20.8'
101+
versionSpec: '20.19.6'
107102
command: custom
108103
verbose: false
109104
customCommand: install autorest@latest

tools/AzDev/src/Typespec/NewTSPModuleCmdlet.cs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public class NewTSPModuleCmdlet : DevCmdletBase
7272
/// Consider updating the logic to use a more unique name if this becomes an issue.
7373
/// </summary>
7474
private const string tempDirName = "TempTypeSpecFiles";
75-
76-
private string _npmPath = "";
7775

7876
[Parameter(HelpMessage = "The location of the TSP config file (can be a URL or local path). Will look for `tsp-location.yaml` in current directory if not provided.")]
7977
public string TSPLocation { get; set; }
@@ -268,19 +266,24 @@ persist tsp-location.yaml to Emitter output directory
268266
File.WriteAllText(Path.Combine(emitterOutputDir, "tsp-location.yaml"), YamlHelper.Serialize(tspLocationData));
269267

270268
string emitterPackageJsonPath = Path.Combine(RepoRoot, "eng", "emitter-package.json");
271-
File.Copy(emitterPackageJsonPath, Path.Combine(Path.GetDirectoryName(tempTSPLocation), "package.json"), true);
269+
tempTSPLocation = Path.GetDirectoryName(tempTSPLocation);
270+
File.Copy(emitterPackageJsonPath, Path.Combine(tempTSPLocation, "package.json"), true);
272271

273272
/*
274273
emit from tempTSPLocation
275274
*/
276275
try
277276
{
278-
InstallDependencies(Path.GetDirectoryName(tempTSPLocation)).Wait();
279-
RunCommand(FindNPMCommandFromPath("tsp"), $"compile ./ --emit {EmitterPath ?? emitterName} --output-dir {emitterOutputDir}", Path.GetDirectoryName(tempTSPLocation)).Wait();
277+
if (!File.Exists(Path.Combine(tempTSPLocation, "package.json")))
278+
{
279+
throw new FileNotFoundException($"package.json not found in {tempTSPLocation}");
280+
}
281+
RunCommand(FindCommandFromPath("npm"), File.Exists(Path.Combine(tempTSPLocation, "package-lock.json")) ? "ci" : "install", tempTSPLocation).Wait();
282+
RunCommand("node", $"{Path.Combine("node_modules", "@typespec", "compiler", "cmd", "tsp")} compile ./ --emit {EmitterPath ?? emitterName} --output-dir {emitterOutputDir}", tempTSPLocation).Wait();
280283
}
281284
catch (Exception ex)
282285
{
283-
throw new InvalidOperationException($"Failed to emit from TSP config [{tempTSPLocation}]: {ex.Message}", ex);
286+
throw new InvalidOperationException($"Failed to emit from TSP config [{Path.Combine(tempTSPLocation, "tspconfig.yaml")}]: {ex.Message}", ex);
284287
}
285288
finally
286289
{
@@ -299,22 +302,34 @@ emit from tempTSPLocation
299302
}
300303
}
301304

302-
private string FindNPMCommandFromPath(string command)
305+
private string FindCommandFromPath(string command)
303306
{
304-
string commandSuffix = Environment.OSVersion.Platform == PlatformID.Win32NT ? ".cmd":"";
305-
if ( string.IsNullOrEmpty(_npmPath) || !File.Exists(_npmPath))
306-
{
307-
string pathEnv = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
308-
string npmPath = pathEnv.Split(Path.PathSeparator).FirstOrDefault(path => path.EndsWith("npm"));
309-
_npmPath = npmPath;
310-
}
311-
string commandPath = Path.Combine(_npmPath, command+commandSuffix);
312-
if (!File.Exists(commandPath))
307+
string pathEnv = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
308+
string[] paths = pathEnv.Split(Path.PathSeparator);
309+
string[] extensions = Environment.OSVersion.Platform == PlatformID.Win32NT
310+
? new[] { ".cmd", ".exe", ".bat", "" }
311+
: new[] { "" };
312+
313+
foreach (string path in paths)
313314
{
314-
315-
throw new FileNotFoundException($"Command '{command}' not found in system PATH.");
315+
foreach (string extension in extensions)
316+
{
317+
try
318+
{
319+
string fullPath = Path.Combine(path, command + extension);
320+
if (File.Exists(fullPath))
321+
{
322+
return fullPath;
323+
}
324+
}
325+
catch
326+
{
327+
// Ignore invalid paths in PATH
328+
}
329+
}
316330
}
317-
return commandPath;
331+
332+
throw new FileNotFoundException($"Command '{command}' not found in system PATH.");
318333
}
319334

320335
private string NormalizePath(string path) => path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
@@ -333,16 +348,6 @@ private void ForceDeleteDir(string path)
333348
Directory.Delete(path, true);
334349
}
335350

336-
private async Task InstallDependencies(string workingDirectory)
337-
{
338-
if (!File.Exists(Path.Combine(workingDirectory, "package.json")))
339-
{
340-
throw new FileNotFoundException($"package.json not found in {workingDirectory}");
341-
}
342-
string args = File.Exists(Path.Combine(workingDirectory, "package-lock.json")) ? "ci" : "install";
343-
await RunCommand(FindNPMCommandFromPath("npm"), args, workingDirectory);
344-
}
345-
346351
private (string, string, string, string) ResolveTSPConfigUri(string uri)
347352
{
348353
Match match = Regex.Match(uri, UriRegex, RegexOptions.IgnoreCase);
@@ -541,15 +546,19 @@ private string ConstructTSPConfigUriFromTSPLocation(string tspLocationPath, (str
541546
{
542547
Dictionary<string, object> tspLocationPWDContent = YamlHelper.Deserialize<Dictionary<string, object>>(File.ReadAllText(tspLocationPath));
543548
//if tspconfig emitted previously was from local, only record the absolute directory name
549+
(string RemoteDirectory, string RemoteCommit, string RemoteRepositoryName, string RemoteForkName) = remoteInfo;
550+
//if tspconfig emitted previously was from local, only record the absolute directory name
544551
if (File.Exists((string)tspLocationPWDContent["directory"]) && string.IsNullOrEmpty((string)tspLocationPWDContent["repo"]) && string.IsNullOrEmpty((string)tspLocationPWDContent["commit"]))
545552
{
546-
if (remoteInfo != (null, null, null, null))
553+
if ( !string.IsNullOrEmpty(RemoteDirectory) ||
554+
!string.IsNullOrEmpty(RemoteCommit) ||
555+
!string.IsNullOrEmpty(RemoteRepositoryName) ||
556+
!string.IsNullOrEmpty(RemoteForkName))
547557
{
548558
throw new ArgumentException("Emitted by local TSP last time, cannot update by remote info. Please provide remote `-TSPLocation`.");
549559
}
550560
return (string)tspLocationPWDContent["directory"];
551561
}
552-
(string RemoteDirectory, string RemoteCommit, string RemoteRepositoryName, string RemoteForkName) = remoteInfo;
553562
//otherwise it was from remote, construct its url
554563
string repo = !string.IsNullOrEmpty(RemoteForkName) ? $"{RemoteForkName}/azure-rest-api-specs" : (!string.IsNullOrEmpty(RemoteRepositoryName) ? RemoteRepositoryName : (string)tspLocationPWDContent["repo"]);
555564
string commit = !string.IsNullOrEmpty(RemoteCommit) ? RemoteCommit : (string)tspLocationPWDContent["commit"];

tools/BuildScripts/BuildScripts.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ function Invoke-SubModuleGeneration {
9393
Set-Location -Path $GenerateDirectory
9494
$tspLocationPath = Join-Path $GenerateDirectory "tsp-location.yaml"
9595
if (Test-Path $tspLocationPath) {
96-
tsp-client update >> $GenerateLog
96+
# Not good practice to do this, this requires 'PrepareAutorestModule.ps1' to prepare AzDev
97+
New-DevTSPModule >> $GenerateLog
9798
}
9899
else {
99100
if ($IsInvokedByPipeline) {

tools/BuildScripts/PrepareAutorestModule.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ if (-not (Test-Path $sourceDirectory)) {
4141
Write-Warning "Cannot find generated directory: $generatedDirectory"
4242
}
4343

44+
if (-Not (Get-Module -Name "AzDev")) {
45+
$AzDevPath = Join-Path $RepoRoot "tools" "AzDev"
46+
$AzDevBuildPath = Join-Path $AzDevPath "build.ps1"
47+
$AzDevModulePath = Join-Path $RepoRoot "artifacts" "AzDev" "AzDev.psd1"
48+
& $AzDevBuildPath
49+
Import-Module $AzDevModulePath -Force
50+
}
51+
4452
$isInvokedByPipeline = $false
4553
if ($InvokedByPipeline) {
4654
$isInvokedByPipeline = $true

0 commit comments

Comments
 (0)