From 09730ad64223da74e5be9194cafe4561142e7775 Mon Sep 17 00:00:00 2001 From: Miodrag Milic Date: Wed, 20 Sep 2017 08:57:23 +0200 Subject: [PATCH] automatic readme --- AU/Public/Set-DescriptionFromReadme.ps1 | 36 ++++++++++++------------- AU/Public/Update-Package.ps1 | 12 ++++++--- CHANGELOG.md | 2 +- README.md | 14 +++++++++- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/AU/Public/Set-DescriptionFromReadme.ps1 b/AU/Public/Set-DescriptionFromReadme.ps1 index 415abe54..9cd19db4 100644 --- a/AU/Public/Set-DescriptionFromReadme.ps1 +++ b/AU/Public/Set-DescriptionFromReadme.ps1 @@ -5,31 +5,29 @@ .DESCRIPTION This script should be called in au_AfterUpdate to put the text in the README.md into description tag of the Nuspec file. The current description will be replaced. - Function will throw an error if README.md is not found. - -.PARAMETER SkipFirst - Number of start lines to skip from the README.md, by default 0. - - -.PARAMETER SkipLast - Number of end lines to skip from the README.md, by default 0. + + You need to call this function manually only if you want to pass it custom parameters. + In that case use NoReadme parameter of the Update-Package. .EXAMPLE - function global:au_AfterUpdate { Set-DescriptionFromReadme -SkipFirst 2 } + function global:au_AfterUpdate { Set-DescriptionFromReadme -Package $args[0] -SkipLast 2 -SkipFirst 2 } #> -function Set-DescriptionFromReadme([int]$SkipFirst=0, [int]$SkipLast=0) { - if (!(Test-Path README.md)) { throw 'Set-DescriptionFromReadme: README.md not found' } +function Set-DescriptionFromReadme{ + param( + [AUPackage] $Package, + # Number of start lines to skip from the README.md, by default 0. + [int] $SkipFirst=0, + # Number of end lines to skip from the README.md, by default 0. + [int] $SkipLast=0 + ) + + 'Setting README.md to Nuspec description tag' - Write-Host 'Setting README.md to Nuspec description tag' $description = gc README.md -Encoding UTF8 $endIdx = $description.Length - $SkipLast $description = $description | select -Index ($SkipFirst..$endIdx) | Out-String + $description = "" - $nuspecFileName = $Latest.PackageName + ".nuspec" - $nu = gc $nuspecFileName -Raw -Encoding UTF8 - $nu = $nu -replace "(?smi)(\).*?(\)", "`${1}$($description)`$2" - - $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False) - $NuPath = (Resolve-Path $NuspecFileName) - [System.IO.File]::WriteAllText($NuPath, $nu, $Utf8NoBomEncoding) + $Package.NuspecXml.package.metadata.description = $description + $Package.SaveNuspec() } diff --git a/AU/Public/Update-Package.ps1 b/AU/Public/Update-Package.ps1 index 7125cdd4..6ddf5cb0 100644 --- a/AU/Public/Update-Package.ps1 +++ b/AU/Public/Update-Package.ps1 @@ -91,8 +91,11 @@ function Update-Package { #Output variable. [string] $Result, - #Backup and restore package - [switch] $WhatIf + #Backup and restore package. + [switch] $WhatIf, + + #Disable automatic update of nuspec description from README.md files with first 2 lines skipped. + [switch] $NoReadme ) function check_urls() { @@ -362,9 +365,10 @@ function Update-Package { if ($WhatIf) { $package.Backup() } try { - if (Test-Path Function:\au_BeforeUpdate) { 'Running au_BeforeUpdate' | result; au_BeforeUpdate | result } + if (Test-Path Function:\au_BeforeUpdate) { 'Running au_BeforeUpdate' | result; au_BeforeUpdate $package | result } update_files - if (Test-Path Function:\au_AfterUpdate) { 'Running au_AfterUpdate' | result; au_AfterUpdate | result } + if (!$NoReadme -and (Test-Path "$($package.Path)\README.md")) { Set-DescriptionFromReadme $package -SkipFirst 2 | result } + if (Test-Path Function:\au_AfterUpdate) { 'Running au_AfterUpdate' | result; au_AfterUpdate $package | result } choco pack --limit-output | result if ($LastExitCode -ne 0) { throw "Choco pack failed with exit code $LastExitCode" } diff --git a/CHANGELOG.md b/CHANGELOG.md index ac80a874..b1dc0ab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Next -- Added new function `Set-DescriptionFromReadme`. +- Added new function `Set-DescriptionFromReadme` that is called automatically when README.md is present in the package folder ([#85](https://github.com/majkinetor/au/issues/85)). ## 2017.8.30 diff --git a/README.md b/README.md index 195598c5..0df8cc06 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ To see AU in action see [video tutorial](https://www.youtube.com/watch?v=m2XpV2L - Use only PowerShell to create automatic update script for given package. - Automatically downloads installers and provides/verifies checksums for x32 and x64 versions. - Verifies URLs, nuspec versions, remote repository existence etc. -- Keep nuspec descriptions in README.md files. +- Automatically sets the Nuspec descriptions from a README.md files. - Can use global variables to change functionality. - Sugar functions for Chocolatey package maintainers. - Update single package or any subset of previously created AU packages with a single command. @@ -138,6 +138,18 @@ Package updated This is best understood via the example - take a look at the real life package [installer script](https://github.com/majkinetor/au-packages/blob/master/dngrep/tools/chocolateyInstall.ps1) and its [AU updater](https://github.com/majkinetor/au-packages/blob/master/dngrep/update.ps1). +### Automatic package description from README.md + +If a package directory contains the `README.md` file, its content will be automatically set as description of the package with first 2 lines omitted from it. + +To disable this option use `$NoReadme` with the `Update-Package` function. You can still call it manually from within `au_AfterUpdate`, which you may want to do in order to pass custom parameters to it: + +```powershell +function global:au_AfterUpdate ($Package) { + Set-DescriptionFromReadme $Package -SkipLast 2 -SkipFirst 5 +} +``` + ### Checks The `update` function does the following checks: