Add script for proper expansion

Closes #3
This commit is contained in:
Andrew Arnott 2019-06-16 22:24:54 -06:00
Родитель 9a6c1df41c
Коммит 26696c117d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: FD21E965BEF82456
11 изменённых файлов: 178 добавлений и 20 удалений

148
Expand-Template.ps1 Normal file
Просмотреть файл

@ -0,0 +1,148 @@
<#
.SYNOPSIS
Expands this template into an actual project, taking values for placeholders
.PARAMETER Name
The name of the library
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$LibraryName,
[Parameter(Mandatory=$true)]
[string]$Author,
[Parameter()]
[string]$CodeCovToken,
[Parameter()]
[string]$CIFeed
)
function Replace-Placeholders {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
$Replacements
)
$Path = Resolve-Path $Path
Write-Host "Replacing tokens in `"$Path`""
$content = Get-Content -Path $Path | Out-String
$Replacements.GetEnumerator() |% {
$modifiedContent = $content -replace $_.Key,$_.Value
if ($modifiedContent -eq $content) {
Write-Error "No $($_.Key) token found to replace."
}
$content = $modifiedContent
}
$content = $content.TrimEnd(("`r","`n"))
[System.IO.File]::WriteAllLines($Path, $content) # Don't use Set-Content because that adds a UTF8 BOM
git add $Path
}
# Try to find sn.exe if it isn't on the PATH
$sn = Get-Command sn -ErrorAction SilentlyContinue
if (-not $sn) {
$snExes = Get-ChildItem -Recurse "${env:ProgramFiles(x86)}\Microsoft SDKs\Windows\sn.exe"
if ($snExes) {
$sn = Get-Command $snExes[0].FullName
} else {
Write-Error "sn command not found on PATH and SDK could not be found."
exit(1)
}
}
# Verify all commands we use are on the PATH
('git','dotnet') |% {
if (-not (Get-Command $_ -ErrorAction SilentlyContinue)) {
Write-Error "$_ command not found on PATH."
exit(1)
}
}
Push-Location $PSScriptRoot
try {
# Rename project directories and solution
Set-Location src
git mv Library.sln "$LibraryName.sln"
git mv Library/Library.csproj "Library/$LibraryName.csproj"
git mv Library "$LibraryName"
git mv Library.Tests/Library.Tests.csproj "Library.Tests/$LibraryName.Tests.csproj"
git mv Library.Tests "$LibraryName.Tests"
# Refresh solution file both to update paths and give the projects unique GUIDs
dotnet sln remove Library/Library.csproj
dotnet sln remove Library.Tests/Library.Tests.csproj
dotnet sln add "$LibraryName"
dotnet sln add "$LibraryName.Tests"
git add "$LibraryName.sln"
# Update project reference in test project. Add before removal to keep the same ItemGroup in place.
dotnet add "$LibraryName.Tests" reference "$LibraryName"
dotnet remove "$LibraryName.Tests" reference Library/Library.csproj
git add "$LibraryName.Tests/$LibraryName.Tests.csproj"
# Establish a new strong-name key
& $sn.Path -k 2048 strongname.snk
git add strongname.snk
Set-Location ..
# Replace placeholders in source files
Replace-Placeholders -Path "src/$LibraryName/Calculator.cs" -Replacements @{
'Library'=$LibraryName
'COMPANY-PLACEHOLDER'=$Author
}
Replace-Placeholders -Path "src/$LibraryName.Tests/CalculatorTests.cs" -Replacements @{
'Library'=$LibraryName
'COMPANY-PLACEHOLDER'=$Author
}
Replace-Placeholders -Path "LICENSE" -Replacements @{
'COMPANY-PLACEHOLDER'=$Author
}
Replace-Placeholders -Path "src/stylecop.json" -Replacements @{
'COMPANY-PLACEHOLDER'=$Author
}
Replace-Placeholders -Path "src/Directory.Build.props" -Replacements @{
'COMPANY-PLACEHOLDER'=$Author
}
Replace-Placeholders -Path "README.md" -Replacements @{
"(?m)^.*\[NuGet package\][^`r`n]*"="[![NuGet package](https://img.shields.io/nuget/v/$LibraryName.svg)](https://nuget.org/packages/$LibraryName)"
"(?m)^.*\[Build Status\].*`r?`n"=""
"(?m)^.*\[codecov\].*`r?`n"=""
}
# Specially handle azure-pipelines.yml edits
$YmlReplacements = @{
"(?m).*expand-template\.yml(?:\r)?\n" = ""
}
if ($CodeCovToken) {
$YmlReplacements['(codecov_token: ).*(#.*)'] = "`$1$CodeCovToken"
} else {
$YmlReplacements['(codecov_token: ).*(#.*)'] = "#`$1`$2"
}
if ($CIFeed) {
$YmlReplacements['(ci_feed: ).*(#.*)'] = "`$1$CIFeed"
} else {
$YmlReplacements['(ci_feed: ).*(#.*)'] = "#`$1`$2"
}
Replace-Placeholders -Path "azure-pipelines.yml" -Replacements $YmlReplacements
# Self destruct
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
git rm Expand-Template.ps1
git rm :/azure-pipelines/expand-template.yml
# Self-integrity check
Get-ChildItem -Recurse -File -Exclude bin,obj,README.md,Expand-Template.ps1 |? { -not $_.FullName.Contains("obj") } |% {
$PLACEHOLDERS = Get-Content -Path $_.FullName |? { $_.Contains('PLACEHOLDER') }
if ($PLACEHOLDERS) {
Write-Error "PLACEHOLDER discovered in $($_.FullName)"
}
}
} finally {
Pop-Location
}
# When testing this script, all the changes can be quickly reverted with this command:
# git reset HEAD :/README.md :/LICENSE :/azure-pipelines.yml :/src :/azure-pipelines; git co -- :/README.md :/LICENSE :/azure-pipelines.yml :/src :/azure-pipelines; git clean -fd :/src

Просмотреть файл

@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) PLACEHOLDER
Copyright (c) COMPANY-PLACEHOLDER
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

Просмотреть файл

@ -20,12 +20,9 @@
## Consumption
Once you've expanded this template for your own use, you should:
Once you've expanded this template for your own use, you should **run the `Expand-Template.ps1` script** to customize the template for your own project.
1. Verify the license is suitable for your goal as it appears in the LICENSE and stylecop.json files
and the Directory.Build.props file's `PackageLicenseExpression` property.
1. Search the repo for all `PLACEHOLDER` occurrences and replace them.
1. Regenerate `src\strongname.snk` file: `sn -k src\strongname.snk`
1. Rename project files, directories, namespaces, and update sln file to match.
1. Reset or remove the `codecov_token` variable in `azure-pipelines.yml`
Further customize your repo by:
1. Verify the license is suitable for your goal as it appears in the LICENSE and stylecop.json files and the Directory.Build.props file's `PackageLicenseExpression` property.
1. Reset or replace the badges at the top of this file.

Просмотреть файл

@ -8,13 +8,14 @@ trigger:
- doc/
- '*.md'
- .vscode/
variables:
TreatWarningsAsErrors: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
BuildConfiguration: Release
BuildPlatform: Any CPU
codecov_token: 4dc9e7e2-6b01-4932-a180-847b52b43d35 # PLACEHOLDER - Get a new one from https://codecov.io/
codecov_token: 4dc9e7e2-6b01-4932-a180-847b52b43d35 # Get a new one from https://codecov.io/
ci_feed: /a5a3bad0-e566-4c53-be83-6458be8d1653 # find guid used by Azure DevOps Artifacts for the feed
jobs:
- job: Windows
@ -31,6 +32,7 @@ jobs:
- template: azure-pipelines/dotnet.yml
- template: azure-pipelines/collect-deployables.yml
- template: azure-pipelines/collect-logs.yml
- template: azure-pipelines/expand-template.yml
- task: NuGetCommand@2
displayName: Push packages to CI feed
@ -38,8 +40,9 @@ jobs:
command: push
packagesToPush: $(Build.ArtifactStagingDirectory)/deployables/*.nupkg;$(Build.ArtifactStagingDirectory)/deployables/*.snupkg
nuGetFeedType: internal
publishVstsFeed: /a5a3bad0-e566-4c53-be83-6458be8d1653 # PLACEHOLDER
publishVstsFeed: $(ci_feed)
allowPackageConflicts: true
condition: ne(variables['ci_feed'], '')
- job: Linux
pool:
@ -48,6 +51,7 @@ jobs:
- template: azure-pipelines/install-dependencies.yml
- template: azure-pipelines/dotnet.yml
- template: azure-pipelines/collect-logs.yml
- template: azure-pipelines/expand-template.yml
- job: macOS
pool:
@ -56,3 +60,4 @@ jobs:
- template: azure-pipelines/install-dependencies.yml
- template: azure-pipelines/dotnet.yml
- template: azure-pipelines/collect-logs.yml
- template: azure-pipelines/expand-template.yml

Просмотреть файл

@ -0,0 +1,10 @@
steps:
- script: git clean -fdx
displayName: Cleaning repo for template expansion
- powershell: ./Expand-Template.ps1 -LibraryName Calc -Author "Andrew Arnott"
displayName: Expanding template
failOnStderr: true
# TODO: Verify that all changes are staged to the git index
- script: dotnet build
workingDirectory: src
displayName: dotnet build (expanded template)

Просмотреть файл

@ -11,8 +11,8 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)\strongname.snk</AssemblyOriginatorKeyFile>
<Company>PLACEHOLDER</Company>
<Authors>PLACEHOLDER</Authors>
<Company>COMPANY-PLACEHOLDER</Company>
<Authors>COMPANY-PLACEHOLDER</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
@ -32,4 +32,4 @@
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>
</Project>

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright (c) PLACEHOLDER. All rights reserved.
// Copyright (c) COMPANY-PLACEHOLDER. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;

Просмотреть файл

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright (c) PLACEHOLDER. All rights reserved.
// Copyright (c) COMPANY-PLACEHOLDER. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Library

Просмотреть файл

@ -3,8 +3,6 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CodeAnalysisRuleSet>..\shipping.ruleset</CodeAnalysisRuleSet>
<Description>PLACEHOLDER</Description>
</PropertyGroup>
</Project>

Просмотреть файл

@ -2,7 +2,7 @@
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
"settings": {
"documentationRules": {
"companyName": "PLACEHOLDER",
"companyName": "COMPANY-PLACEHOLDER",
"copyrightText": "Copyright (c) {companyName}. All rights reserved.\nLicensed under the {licenseName} license. See {licenseFile} file in the project root for full license information.",
"variables": {
"licenseName": "MIT",
@ -12,4 +12,4 @@
"xmlHeader": false
}
}
}
}