Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cadl project scripts to common #5231

Merged
6 commits merged into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions eng/common/scripts/Cadl-Project-Generate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[CmdletBinding()]
param (
[Parameter(Position=0)]
[ValidateNotNullOrEmpty()]
[string] $ProjectDirectory
)

$ErrorActionPreference = "Stop"
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
. $PSScriptRoot/common.ps1
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module

function NpmInstallForProject([string]$workingDirectory) {
Push-Location $workingDirectory
try {
$currentDur = Resolve-Path "."
Write-Host "Generating from $currentDur"
if (Test-Path "package.json") {
Remove-Item -Path "package.json" -Force
}
if (Test-Path ".npmrc") {
m-nash marked this conversation as resolved.
Show resolved Hide resolved
Remove-Item -Path ".npmrc" -Force
}
$replacementPackageJson = "$PSScriptRoot/../../emitter-package.json"
m-nash marked this conversation as resolved.
Show resolved Hide resolved
Write-Host("Copying package.json from $replacementPackageJson")
Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force
npm install
m-nash marked this conversation as resolved.
Show resolved Hide resolved
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}
finally {
Pop-Location
}
}

$emitterName = &$GetEmitterNameFn
$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml"

Write-Host "Reading configuration from $cadlConfigurationFile"
$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml

$specSubDirectory = $configuration["directory"]
$innerFolder = Split-Path $specSubDirectory -Leaf

$tempFolder = "$ProjectDirectory/TempCadlFiles"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there isn't a good reason to have the Temp folder in the same directory it would be nice come up with some other convention, maybe even the global temp folder.

If we keep it under the repo we will need to get it added to .gitignore in the other repos as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is added to .gitignore and the advantage here is if you want to do some local changes / what if having it right next to your project is convenient

$npmWorkingDir = Resolve-Path $tempFolder/$innerFolder
$mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npmWorkingDir/client.cadl" } Else { Resolve-Path "$npmWorkingDir/main.cadl"}

try {
Push-Location $npmWorkingDir
NpmInstallForProject $npmWorkingDir

if ($LASTEXITCODE) { exit $LASTEXITCODE }

$emitterAdditionalOptions = &$GetEmitterAdditionalOptionsFn
Write-Host("npx cadl compile $mainCadlFile --emit $emitterName $emitterAdditionalOptions")
npx cadl compile $mainCadlFile --emit $emitterName $emitterAdditionalOptions

if ($LASTEXITCODE) { exit $LASTEXITCODE }
}
finally {
Pop-Location
}

$shouldCleanUp = $configuration["cleanup"] ?? $true
if ($shouldCleanUp) {
Remove-Item $tempFolder -Recurse -Force
}
138 changes: 138 additions & 0 deletions eng/common/scripts/Cadl-Project-Sync.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[CmdletBinding()]
param (
[Parameter(Position=0)]
[ValidateNotNullOrEmpty()]
[string] $ProjectDirectory
)

$ErrorActionPreference = "Stop"
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
$sparseCheckoutFile = ".git/info/sparse-checkout"

function AddSparseCheckoutPath([string]$subDirectory) {
if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) {
Write-Output $subDirectory >> .git/info/sparse-checkout
}
}

function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) {
$source = "$specCloneRoot/$mainSpecDir"
Write-Host "Copying spec from $source"
# $mainSpecDir is the PR folder, we just need to copy its subfolders which include the cadl project folder
Get-ChildItem –Path "$source" -Exclude @("data-plane", "resource-manager")|
m-nash marked this conversation as resolved.
Show resolved Hide resolved
Foreach-Object {
Copy-Item -Path $_.FullName -Destination $dest -Recurse -Force
}
foreach($additionalDir in $specAdditionalSubDirectories)
{
$source = "$specCloneRoot/$additionalDir"
Write-Host "Copying spec from $source"
Copy-Item -Path $source -Destination $dest -Recurse -Force
}
}

function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) {
AddSparseCheckoutPath $mainSpecDir
foreach($subDir in $specAdditionalSubDirectories)
{
AddSparseCheckoutPath $subDir
}
}

function GetGitRemoteValue([string]$repo) {
Push-Location $ProjectDirectory
$result = ""
try {
$gitRemotes = (git remote -v)
foreach ($remote in $gitRemotes)
{
if ($remote.StartsWith("origin")) {
if ($remote -match 'https://github.com/\S+[\.git]') {
$result = "https://github.com/$repo.git"
break
} elseif ($remote -match "[email protected]:\S+[\.git]"){
$result = "[email protected]:$repo.git"
break
} else {
throw "Unknown git remote format found: $remote"
}
}
}
}
finally {
Pop-Location
}

return $result
}

function InitializeSparseGitClone([string]$repo) {
git clone --no-checkout --filter=tree:0 $repo .
if ($LASTEXITCODE) { exit $LASTEXITCODE }
git sparse-checkout init
if ($LASTEXITCODE) { exit $LASTEXITCODE }
Remove-Item $sparseCheckoutFile -Force
}

function GetSpecCloneDir([string]$projectName) {
Push-Location $ProjectDirectory
try {
$root = git rev-parse --show-toplevel
}
finally {
Pop-Location
}

$sparseSpecCloneDir = "$root/../sparse-spec/$projectName"
New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null
$createResult = Resolve-Path $sparseSpecCloneDir
return $createResult
}

$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml"
Write-Host "Reading configuration from $cadlConfigurationFile"
$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml

$pieces = $cadlConfigurationFile.Path.Replace("\","/").Split("/")
$projectName = $pieces[$pieces.Count - 3]

# clone the whole RP directory which is the parent of $configuration["directory"]
if ($configuration["directory"] -match "^[^/\\]+[\\/]+[^/\\]+")
{
$specSubDirectory = $Matches[0]
}
else
{
throw "The directory in $cadlConfigurationFile is not expected"
}
if ( $configuration["repo"] -and $configuration["commit"]) {
$specCloneDir = GetSpecCloneDir $projectName
$gitRemoteValue = GetGitRemoteValue $configuration["repo"]

Write-Host "Setting up sparse clone for $projectName at $specCloneDir"

Push-Location $specCloneDir.Path
try {
if (!(Test-Path ".git")) {
InitializeSparseGitClone $gitRemoteValue
UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"]
}
git checkout $configuration["commit"]
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}
finally {
Pop-Location
}
} elseif ( $configuration["spec-root-dir"] ) {
$specCloneDir = $configuration["spec-root-dir"]
}


$tempCadlDir = "$ProjectDirectory/TempCadlFiles"
New-Item $tempCadlDir -Type Directory -Force | Out-Null
CopySpecToProjectIfNeeded `
-specCloneRoot $specCloneDir `
-mainSpecDir $specSubDirectory `
-dest $tempCadlDir `
-specAdditionalSubDirectories $configuration["additionalDirectories"]
2 changes: 2 additions & 0 deletions eng/common/scripts/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ $GetDocsMsTocChildrenForManagementPackagesFn = "Get-${Language}-DocsMsTocChildre
$UpdateDocsMsTocFn = "Get-${Language}-UpdatedDocsMsToc"
$GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme"
$GetRepositoryLinkFn = "Get-${Language}-RepositoryLink"
$GetEmitterAdditionalOptionsFn = "Get-${Language}-EmitterAdditionalOptions"
$GetEmitterNameFn = "Get-${Language}-EmitterName"