Skip to content

Commit

Permalink
Fixed generation of PDF and cheat sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbartel committed Nov 17, 2019
1 parent 6ea8028 commit f332d42
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 93 deletions.
82 changes: 69 additions & 13 deletions Build/default.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
$BaseDirectory = Resolve-Path ..
$ArtifactsDirectory = "$BaseDirectory\Artifacts\"
$LibDir = "$BaseDirectory\Lib"
$defaultRulePrefix = "AV"
}

task default -depends Clean, ExtractVersionsFromGit, Compile, CompileCheatsheet, BuildHtml
Expand Down Expand Up @@ -49,27 +50,78 @@ task Compile {

$outfile = "$ArtifactsDirectory\Guidelines\CSharpCodingGuidelines.md"

foreach ($file in $files) {
Write-Host "Including " $file
$content = Get-Content $file | Out-String

$content = $content.replace('%semver%', $script:Semver)
$content = $content.replace('%commitdate%', $script:CommitDate)
$content = $content.replace('![](/assets', '![](assets')
foreach ($file in $files) {
$rawContent = Get-Content $file | Out-String

$rawContent = $rawContent.replace('%semver%', $script:Semver)
$rawContent = $rawContent.replace('%commitdate%', $script:CommitDate)
$rawContent = $rawContent.replace('![](/assets', '![](assets')

# Extract the title of the section from the Frontmatter block
if ($content -match "---(.|\n)*title\: (.+)") {
$title = $Matches[2]
$title = ""
if ($rawContent -match "---(.|\n)*title\: (.+)") {
$title = $Matches[2].Trim()
}
# Extract the category from the Frontmatter block
$category = ""
if ($rawContent -match "---(.|\n)*rule_category\: (.+)") {
$category = $Matches[2].Trim()
}

# Remove the entire Frontmatter block
$content = ($content -replace '---\r?\n(.|\r?\n)+?---\r?\n', "")
$rawContent = ($rawContent -replace '---\r?\n(.|\r?\n)+?---\r?\n', "")

# Extract the category from the Frontmatter block
if ([string]::IsNullOrEmpty($category)) {
Write-Host "Including " $file
$content = $rawContent
} else {
Write-Host "Including rules of category " $category
$content = ""
foreach ($ruleFile in (Get-ChildItem "$BaseDirectory\_rules\*")) {
$rule = Get-Content $ruleFile.FullName | Out-String
if ($rule -match "---(.|\n)*rule_category\: $category") {
# Extract the title of the rule from the Frontmatter block
$ruleTitle = ""
if ($rule -match "---(.|\n)*title\: (.+)") {
$ruleTitle = $Matches[2].Trim()
}

# Extract the severity of the rule from the Frontmatter block
$ruleSeverity = ""
if ($rule -match "---(.|\n)*severity\: (.+)") {
$ruleSeverity = $Matches[2].Trim()
}

# Extract the id of the rule from the Frontmatter block
$ruleId = ""
if ($rule -match "---(.|\n)*rule_id\: (.+)") {
$ruleId = $Matches[2].Trim()
}

# Extract the id prefix of the rule from the Frontmatter block
$ruleIdPrefix = "{{ site.default_rule_prefix }}"
if ($rule -match "---(.|\n)*custom_prefix\: (.+)") {
$ruleIdPrefix = $Matches[2].Trim()
}

$content += "<h3 id=`"${ruleIdPrefix}${ruleId}`">$ruleTitle (${ruleIdPrefix}${ruleId}) <img src=`"assets/images/$ruleSeverity.png`" /></h3>`n"

# Add rule content without Frontmatter
$content += ($rule -replace '---\r?\n(.|\r?\n)+?---\r?\n', "")
$content += "`n"
}
}
}

# Replace rule prefix variable
$content = ($content -replace '{{ site.default_rule_prefix }}', $defaultRulePrefix)

# Replace cross-page relative links with local links (since everything becomes a single HTML)
$content = ($content -replace '\(\/.+?(#av\d+)\)', '($1)')
$content = ($content -replace '\(\/.+?(#\w+)\)', '($1)')

if ($title) {
$content = "<h1>$title</h1>" + $content;
$content = "<h1>$title</h1>`n" + $content;
}

Add-Content -Path $outfile $content
Expand All @@ -86,7 +138,11 @@ task CompileCheatsheet {

$outfile = "$ArtifactsDirectory\Cheatsheet\Cheatsheet.md"

(Get-Content "$BaseDirectory\_pages\Cheatsheet.md").replace('%semver%', $script:Semver).replace('%commitdate%', $script:CommitDate) | Add-Content $outfile
$content = Get-Content "$BaseDirectory\_pages\Cheatsheet.md"
$content = ($content -replace '%semver%', $script:Semver)
$content = ($content -replace '%commitdate%', $script:CommitDate)
$content = ($content -replace '{{ site.default_rule_prefix }}', $defaultRulePrefix)
Add-Content $outfile $content

Copy-Item -Path "$BaseDirectory\assets\css\CheatSheet.css" -Destination "$ArtifactsDirectory\Cheatsheet\style.css" -recurse -Force
Copy-Item -Path "$BaseDirectory\assets\Images" -Destination "$ArtifactsDirectory\Cheatsheet\Assets\Images" -recurse -Force
Expand Down
160 changes: 80 additions & 80 deletions _pages/Cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,83 +28,83 @@ NOTE: Requires Markdown Extra. See http://michelf.ca/projects/php-markdown/extra

**Class Design**

* A class or interface should have a single purpose (AV1000)
* An interface should be small and focused (AV1003)
* Use an interface to decouple classes from each other (AV1005)
* Don't suppress compiler warnings using the `new` keyword (AV1010)
* It should be possible to treat a derived object as if it were a base class object (AV1011)
* Don't refer to derived classes from the base class (AV1013)
* Avoid exposing the other objects an object depends on (AV1014)
* Avoid bidirectional dependencies (AV1020)
* Classes should have state and behavior (AV1025)
* Classes should protect the consistency of their internal state (AV1026)
* A class or interface should have a single purpose ({{ site.default_rule_prefix }}1000)
* An interface should be small and focused ({{ site.default_rule_prefix }}1003)
* Use an interface to decouple classes from each other ({{ site.default_rule_prefix }}1005)
* Don't suppress compiler warnings using the `new` keyword ({{ site.default_rule_prefix }}1010)
* It should be possible to treat a derived object as if it were a base class object ({{ site.default_rule_prefix }}1011)
* Don't refer to derived classes from the base class ({{ site.default_rule_prefix }}1013)
* Avoid exposing the other objects an object depends on ({{ site.default_rule_prefix }}1014)
* Avoid bidirectional dependencies ({{ site.default_rule_prefix }}1020)
* Classes should have state and behavior ({{ site.default_rule_prefix }}1025)
* Classes should protect the consistency of their internal state ({{ site.default_rule_prefix }}1026)

<br/>
**Member Design**

* Allow properties to be set in any order (AV1100)
* Don't use mutually exclusive properties (AV1110)
* A property, method or local function should do only one thing (AV1115)
* Don't expose stateful objects through static members (AV1125)
* Return an `IEnumerable<T>` or `ICollection<T>` instead of a concrete collection class (AV1130)
* Properties, arguments and return values representing strings, collections or tasks should never be `null` (AV1135)
* Define parameters as specific as possible (AV1137)
* Allow properties to be set in any order ({{ site.default_rule_prefix }}1100)
* Don't use mutually exclusive properties ({{ site.default_rule_prefix }}1110)
* A property, method or local function should do only one thing ({{ site.default_rule_prefix }}1115)
* Don't expose stateful objects through static members ({{ site.default_rule_prefix }}1125)
* Return an `IEnumerable<T>` or `ICollection<T>` instead of a concrete collection class ({{ site.default_rule_prefix }}1130)
* Properties, arguments and return values representing strings, collections or tasks should never be `null` ({{ site.default_rule_prefix }}1135)
* Define parameters as specific as possible ({{ site.default_rule_prefix }}1137)
</td>
<td class="column">
**Miscellaneous Design**

* Throw exceptions rather than returning some kind of status value (AV1200)
* Provide a rich and meaningful exception message text (AV1202)
* Don't swallow errors by catching generic exceptions (AV1210)
* Properly handle exceptions in asynchronous code (AV1215)
* Always check an event handler delegate for `null` (AV1220)
* Use a protected virtual method to raise each event (AV1225)
* Don't pass `null` as the `sender` argument when raising an event (AV1235)
* Use generic constraints if applicable (AV1240)
* Evaluate the result of a LINQ expression before returning it (AV1250)
* Do not use `this` and `base` prefixes unless it is required (AV1251)
* Throw exceptions rather than returning some kind of status value ({{ site.default_rule_prefix }}1200)
* Provide a rich and meaningful exception message text ({{ site.default_rule_prefix }}1202)
* Don't swallow errors by catching generic exceptions ({{ site.default_rule_prefix }}1210)
* Properly handle exceptions in asynchronous code ({{ site.default_rule_prefix }}1215)
* Always check an event handler delegate for `null` ({{ site.default_rule_prefix }}1220)
* Use a protected virtual method to raise each event ({{ site.default_rule_prefix }}1225)
* Don't pass `null` as the `sender` argument when raising an event ({{ site.default_rule_prefix }}1235)
* Use generic constraints if applicable ({{ site.default_rule_prefix }}1240)
* Evaluate the result of a LINQ expression before returning it ({{ site.default_rule_prefix }}1250)
* Do not use `this` and `base` prefixes unless it is required ({{ site.default_rule_prefix }}1251)

<br/>
**Maintainability**

* Methods should not exceed 7 statements (AV1500)
* Make all members `private` and types `internal sealed` by default (AV1501)
* Avoid conditions with double negatives (AV1502)
* Don't use "magic" numbers (AV1515)
* Only use `var` when the type is very obvious (AV1520)
* Declare and initialize variables as late as possible (AV1521)
* Assign each variable in a separate statement (AV1522)
* Favor object and collection initializers over separate statements (AV1523)
* Don't make explicit comparisons to `true` or `false` (AV1525)
* Don't change a loop variable inside a `for` loop (AV1530)
* Avoid nested loops (AV1532)
* Methods should not exceed 7 statements ({{ site.default_rule_prefix }}1500)
* Make all members `private` and types `internal sealed` by default ({{ site.default_rule_prefix }}1501)
* Avoid conditions with double negatives ({{ site.default_rule_prefix }}1502)
* Don't use "magic" numbers ({{ site.default_rule_prefix }}1515)
* Only use `var` when the type is very obvious ({{ site.default_rule_prefix }}1520)
* Declare and initialize variables as late as possible ({{ site.default_rule_prefix }}1521)
* Assign each variable in a separate statement ({{ site.default_rule_prefix }}1522)
* Favor object and collection initializers over separate statements ({{ site.default_rule_prefix }}1523)
* Don't make explicit comparisons to `true` or `false` ({{ site.default_rule_prefix }}1525)
* Don't change a loop variable inside a `for` loop ({{ site.default_rule_prefix }}1530)
* Avoid nested loops ({{ site.default_rule_prefix }}1532)
</td>
<td class="column">
* Always add a block after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` (AV1535)
* Always add a `default` block after the last `case` in a `switch` statement (AV1536)
* Finish every `if`-`else`-`if` statement with an `else` clause (AV1537)
* Be reluctant with multiple `return` statements (AV1540)
* Don't use an `if`-`else` construct instead of a simple (conditional) assignment (AV1545)
* Encapsulate complex expressions in a property, method or local function (AV1547)
* Call the more overloaded method from other overloads (AV1551)
* Only use optional parameters to replace overloads (AV1553)
* Do not use optional parameters in interface methods or their concrete implementations (AV1554)
* Avoid using named arguments (AV1555)
* Don't declare signatures with more than 3 parameters (AV1561)
* Don't use `ref` or `out` parameters (AV1562)
* Avoid signatures that take a `bool` flag (AV1564)
* Prefer `is` patterns over `as` operations (AV1570)
* Don't comment out code (AV1575)
* Always add a block after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` ({{ site.default_rule_prefix }}1535)
* Always add a `default` block after the last `case` in a `switch` statement ({{ site.default_rule_prefix }}1536)
* Finish every `if`-`else`-`if` statement with an `else` clause ({{ site.default_rule_prefix }}1537)
* Be reluctant with multiple `return` statements ({{ site.default_rule_prefix }}1540)
* Don't use an `if`-`else` construct instead of a simple (conditional) assignment ({{ site.default_rule_prefix }}1545)
* Encapsulate complex expressions in a property, method or local function ({{ site.default_rule_prefix }}1547)
* Call the more overloaded method from other overloads ({{ site.default_rule_prefix }}1551)
* Only use optional parameters to replace overloads ({{ site.default_rule_prefix }}1553)
* Do not use optional parameters in interface methods or their concrete implementations ({{ site.default_rule_prefix }}1554)
* Avoid using named arguments ({{ site.default_rule_prefix }}1555)
* Don't declare signatures with more than 3 parameters ({{ site.default_rule_prefix }}1561)
* Don't use `ref` or `out` parameters ({{ site.default_rule_prefix }}1562)
* Avoid signatures that take a `bool` flag ({{ site.default_rule_prefix }}1564)
* Prefer `is` patterns over `as` operations ({{ site.default_rule_prefix }}1570)
* Don't comment out code ({{ site.default_rule_prefix }}1575)

<br/>
**Framework Guidelines**

* Use C# type aliases instead of the types from the `System` namespace (AV2201)
* Prefer language syntax over explicit calls to underlying implementations (AV2202)
* Build with the highest warning level (AV2210)
* Use lambda expressions instead of anonymous methods (AV2221)
* Only use the `dynamic` keyword when talking to a dynamic object (AV2230)
* Favor `async`/`await` over `Task` continuations (AV2235)
* Use C# type aliases instead of the types from the `System` namespace ({{ site.default_rule_prefix }}2201)
* Prefer language syntax over explicit calls to underlying implementations ({{ site.default_rule_prefix }}2202)
* Build with the highest warning level ({{ site.default_rule_prefix }}2210)
* Use lambda expressions instead of anonymous methods ({{ site.default_rule_prefix }}2221)
* Only use the `dynamic` keyword when talking to a dynamic object ({{ site.default_rule_prefix }}2230)
* Favor `async`/`await` over `Task` continuations ({{ site.default_rule_prefix }}2235)
</td>
<tr>

Expand Down Expand Up @@ -162,31 +162,31 @@ NOTE: Requires Markdown Extra. See http://michelf.ca/projects/php-markdown/extra
<br/>
**Naming**

* Use US English (AV1701)
* Don't prefix fields (AV1705)
* Don't use abbreviations (AV1706)
* Name members, parameters and variables according to their meaning and not their type (AV1707)
* Name types using nouns, noun phrases or adjective phrases (AV1708)
* Name generic type parameters with descriptive names (AV1709)
* Don't repeat the name of a class or enumeration in its members (AV1710)
* Avoid short names or names that can be mistaken for other names (AV1712)
* Properly name properties (AV1715)
* Name methods and local functions using verbs or verb-object pairs (AV1720)
* Use a verb or verb phrase to name an event (AV1735)
* Postfix asynchronous methods with `Async` or `TaskAsync` (AV1755)
* Use US English ({{ site.default_rule_prefix }}1701)
* Don't prefix fields ({{ site.default_rule_prefix }}1705)
* Don't use abbreviations ({{ site.default_rule_prefix }}1706)
* Name members, parameters and variables according to their meaning and not their type ({{ site.default_rule_prefix }}1707)
* Name types using nouns, noun phrases or adjective phrases ({{ site.default_rule_prefix }}1708)
* Name generic type parameters with descriptive names ({{ site.default_rule_prefix }}1709)
* Don't repeat the name of a class or enumeration in its members ({{ site.default_rule_prefix }}1710)
* Avoid short names or names that can be mistaken for other names ({{ site.default_rule_prefix }}1712)
* Properly name properties ({{ site.default_rule_prefix }}1715)
* Name methods and local functions using verbs or verb-object pairs ({{ site.default_rule_prefix }}1720)
* Use a verb or verb phrase to name an event ({{ site.default_rule_prefix }}1735)
* Postfix asynchronous methods with `Async` or `TaskAsync` ({{ site.default_rule_prefix }}1755)
</td>
<td class="column">

* Use an underscore for irrelevant parameters (AV1739)
* Use an underscore for irrelevant parameters ({{ site.default_rule_prefix }}1739)


**Documentation**

* Write comments and documentation in US English (AV2301)
* Document all `public`, `protected` and `internal` types and members (AV2305)
* Write XML documentation with other developers in mind (AV2306)
* Avoid inline comments (AV2310)
* Only write comments to explain complex algorithms or decisions (AV2316)
* Write comments and documentation in US English ({{ site.default_rule_prefix }}2301)
* Document all `public`, `protected` and `internal` types and members ({{ site.default_rule_prefix }}2305)
* Write XML documentation with other developers in mind ({{ site.default_rule_prefix }}2306)
* Avoid inline comments ({{ site.default_rule_prefix }}2310)
* Only write comments to explain complex algorithms or decisions ({{ site.default_rule_prefix }}2316)
<br/>

**Layout**
Expand All @@ -195,7 +195,7 @@ NOTE: Requires Markdown Extra. See http://michelf.ca/projects/php-markdown/extra
* Indent 4 spaces, don't use tabs
* Keep one space between keywords like `if` and the expression, but don't add spaces after `(` and before `)`
* Add a space around operators, like `+`, `-`, `==`, etc.
* Always add curly braces after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` (AV1535)
* Always add curly braces after the keywords `if`, `else`, `do`, `while`, `for`, `foreach` and `case` ({{ site.default_rule_prefix }}1535)
* Always put opening and closing curly braces on a new line
* Don't indent object/collection initializers and initialize each property on a new line
* Don't indent lambda statement blocks
Expand All @@ -204,8 +204,8 @@ NOTE: Requires Markdown Extra. See http://michelf.ca/projects/php-markdown/extra
* Add parentheses around every binary expression, but don't add parentheses around unary expressions
<br/>

* Do not use `#region` (AV2407)
* Use expression-bodied members appropriately (AV2410)
* Do not use `#region` ({{ site.default_rule_prefix }}2407)
* Use expression-bodied members appropriately ({{ site.default_rule_prefix }}2410)
</td>
<td class="column">

Expand Down

0 comments on commit f332d42

Please sign in to comment.