Skip to content

Commit

Permalink
automatic readme
Browse files Browse the repository at this point in the history
  • Loading branch information
majkinetor committed Sep 20, 2017
1 parent 040063b commit 09730ad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
36 changes: 17 additions & 19 deletions AU/Public/Set-DescriptionFromReadme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<![CDATA[" + $description + "]]>"

$nuspecFileName = $Latest.PackageName + ".nuspec"
$nu = gc $nuspecFileName -Raw -Encoding UTF8
$nu = $nu -replace "(?smi)(\<description\>).*?(\</description\>)", "`${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()
}
12 changes: 8 additions & 4 deletions AU/Public/Update-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 09730ad

Please sign in to comment.