Skip to content

Commit

Permalink
Update, simplify, and consolidate build scripts (#1082)
Browse files Browse the repository at this point in the history
* consolidate build of script analyzer into module

Require any 2.1 sdk for building, this way we can build with the same sdk as PowerShell core

* Have git ignore vim swap files and the test results file

* Only special case linux for the customized rule tests

* Update to build scripts.

To build for core, simply type ./build,
to build all flavors and documentation type ./build -all.
You can now run tests as well via ./build -Test.

* Modify readme to match new build instructions

* Add ubuntu16 docker file to build scripts

* change to use psversion rather than analyzerversion

because it's really about the version of PS, not the analyzer

* Add copyright banners to script and module

* Fix repo to point to the official repo

* Update appveyor scripts to use new build script

* Forcing installation of platyPS if not found when building documentation

* Be sure to skip uses of TypeDefinitionAst on both PSV3 and PSV4

* Modified logic to not build full on non-Windows

Add additional verbose statements for debugging

* fix issue with incorrect build configuration

When simplifying to use only psversion, add back the 'PSV' prolog to configuration

* reduce build matrix to accellerate CI testing

* Revert "reduce build matrix to accellerate CI testing"

This reverts commit b3018ea.
  • Loading branch information
JamesWTruher authored and bergmeister committed Oct 23, 2018
1 parent 2df52ab commit 1eb9a01
Show file tree
Hide file tree
Showing 18 changed files with 460 additions and 361 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,10 @@ FakesAssemblies/
*.opt

##Our project binplace location
PSScriptAnalyzer/
PSScriptAnalyzer/

# Vim swap files
*.swp

# Test result file
TestResults.xml
2 changes: 1 addition & 1 deletion Engine/Generic/RuleSuppression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
targetAsts = scopeAst.FindAll(item => item is FunctionDefinitionAst && reg.IsMatch((item as FunctionDefinitionAst).Name), true);
goto default;

#if !PSV3
#if !(PSV3||PSV4)

case "class":
targetAsts = scopeAst.FindAll(item => item is TypeDefinitionAst && reg.IsMatch((item as TypeDefinitionAst).Name), true);
Expand Down
24 changes: 12 additions & 12 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public bool IsDscResourceClassBased(ScriptBlockAst ast)
return false;
}

#if !PSV3
#if !(PSV3||PSV4)

List<string> dscResourceFunctionNames = new List<string>(new string[] { "Test", "Get", "Set" });

Expand Down Expand Up @@ -1018,7 +1018,7 @@ internal VariableAnalysis InitializeVariableAnalysisHelper(Ast ast, VariableAnal
/// <param name="scriptAst"></param>
/// <returns></returns>

#if PSV3
#if (PSV3||PSV4)

public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret)

Expand Down Expand Up @@ -1089,7 +1089,7 @@ public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret,
/// <param name="classes"></param>
/// <returns></returns>

#if PSV3
#if (PSV3||PSV4)

public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast scopeAst)

Expand All @@ -1106,7 +1106,7 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast

VariableAnalysisDetails details = null;

#if !PSV3
#if !(PSV3||PSV4)

TypeDefinitionAst psClass = null;

Expand Down Expand Up @@ -1149,7 +1149,7 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast
/// <param name="analysisDetails"></param>
/// <returns></returns>

#if PSV3
#if (PSV3||PSV4)

internal string GetTypeFromMemberExpressionAstHelper(MemberExpressionAst memberAst, VariableAnalysisDetails analysisDetails)

Expand All @@ -1162,7 +1162,7 @@ internal string GetTypeFromMemberExpressionAstHelper(MemberExpressionAst memberA
//Try to get the type without using psClass first
Type result = AssignmentTarget.GetTypeFromMemberExpressionAst(memberAst);

#if !PSV3
#if !(PSV3||PSV4)

//If we can't get the type, then it may be that the type of the object being invoked on is a powershell class
if (result == null && psClass != null && analysisDetails != null)
Expand Down Expand Up @@ -1270,7 +1270,7 @@ public Dictionary<string, List<RuleSuppression>> GetRuleSuppression(Ast ast)
ruleSuppressionList.AddRange(GetSuppressionsFunction(funcAst));
}

#if !PSV3
#if !(PSV3||PSV4)
// Get rule suppression from classes
IEnumerable<TypeDefinitionAst> typeAsts = ast.FindAll(item => item is TypeDefinitionAst, true).Cast<TypeDefinitionAst>();

Expand Down Expand Up @@ -1322,7 +1322,7 @@ internal List<RuleSuppression> GetSuppressionsFunction(FunctionDefinitionAst fun
return result;
}

#if !PSV3
#if !(PSV3||PSV4)
/// <summary>
/// Returns a list of rule suppression from the class
/// </summary>
Expand Down Expand Up @@ -2039,7 +2039,7 @@ private object VisitStatementHelper(StatementAst statementAst)
return null;
}

#if PSV3
#if (PSV3||PSV4)

statementAst.Visit(this);

Expand Down Expand Up @@ -2755,7 +2755,7 @@ public class FindPipelineOutput : ICustomAstVisitor
{
List<Tuple<string, StatementAst>> outputTypes;

#if !PSV3
#if !(PSV3||PSV4)

IEnumerable<TypeDefinitionAst> classes;

Expand Down Expand Up @@ -2797,7 +2797,7 @@ static FindPipelineOutput()
/// </summary>
/// <param name="ast"></param>

#if PSV3
#if (PSV3||PSV4)

public FindPipelineOutput(FunctionDefinitionAst ast)

Expand Down Expand Up @@ -2828,7 +2828,7 @@ public FindPipelineOutput(FunctionDefinitionAst ast, IEnumerable<TypeDefinitionA
/// </summary>
/// <returns></returns>

#if PSV3
#if (PSV3||PSV4)

public static List<Tuple<string, StatementAst>> OutputTypes(FunctionDefinitionAst funcAst)
{
Expand Down
2 changes: 1 addition & 1 deletion Engine/VariableAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void AnalyzeImpl(Ast ast, VariableAnalysis outerAnalysis)
parent = parent.Parent;
}

#if !PSV3
#if !(PSV3||PSV4)

List<TypeDefinitionAst> classes = parent.FindAll(item =>
item is TypeDefinitionAst && (item as TypeDefinitionAst).IsClass, true)
Expand Down
16 changes: 8 additions & 8 deletions Engine/VariableAnalysisBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal void InitializeVariables(Ast ast)
_variables.Add("true", new VariableAnalysisDetails { Name = "true", RealName = "true", Type = typeof(bool) });
_variables.Add("false", new VariableAnalysisDetails { Name = "false", RealName = "true", Type = typeof(bool) });

#if !PSV3
#if !(PSV3||PSV4)

if (ast is FunctionMemberAst)
{
Expand Down Expand Up @@ -808,7 +808,7 @@ internal static void InitializeSSA(Dictionary<string, VariableAnalysisDetails> V
/// <param name="Entry"></param>
/// <param name="Classes"></param>
/// <returns></returns>
#if PSV3
#if (PSV3||PSV4)

internal static Tuple<Dictionary<string, VariableAnalysisDetails>, Dictionary<string, VariableAnalysisDetails>> SparseSimpleConstants(
Dictionary<string, VariableAnalysisDetails> Variables, Block Entry)
Expand Down Expand Up @@ -989,7 +989,7 @@ internal static Tuple<Dictionary<string, VariableAnalysisDetails>, Dictionary<st
{
VariableAnalysisDetails analysis = VariablesDictionary[VariableAnalysis.AnalysisDictionaryKey(memAst.Expression as VariableExpressionAst)];

#if PSV3
#if (PSV3||PSV4)

Type possibleType = AssignmentTarget.GetTypeFromMemberExpressionAst(memAst, analysis);

Expand Down Expand Up @@ -1371,7 +1371,7 @@ public AssignmentTarget(string variableName, Type type)
/// <param name="psClass"></param>
/// <returns></returns>

#if PSV3
#if (PSV3||PSV4)

internal static Type GetTypeFromMemberExpressionAst(MemberExpressionAst memAst, VariableAnalysisDetails analysis)

Expand Down Expand Up @@ -1460,7 +1460,7 @@ internal static Type GetTypeFromMemberExpressionAst(MemberExpressionAst memberAs
// isStatic is true
result = GetTypeFromInvokeMemberAst(type, imeAst, methodName, true);
}
#if !PSV3
#if !(PSV3||PSV4)
else
{
// Check for classes
Expand Down Expand Up @@ -1498,7 +1498,7 @@ internal static Type GetTypeFromMemberExpressionAst(MemberExpressionAst memberAs
{
result = GetPropertyOrFieldTypeFromMemberExpressionAst(expressionType, fieldName);
}
#if !PSV3
#if !(PSV3||PSV4)
else
{
// check for class type
Expand Down Expand Up @@ -1531,7 +1531,7 @@ internal static Type GetTypeFromMemberExpressionAst(MemberExpressionAst memberAs
if (memberAst.Expression is VariableExpressionAst
&& String.Equals((memberAst.Expression as VariableExpressionAst).VariablePath.UserPath, "this", StringComparison.OrdinalIgnoreCase))
{
#if !PSV3
#if !(PSV3||PSV4)

// Check that we are in a class
TypeDefinitionAst psClass = FindClassAncestor(memberAst);
Expand Down Expand Up @@ -1598,7 +1598,7 @@ internal static Type GetPropertyOrFieldTypeFromMemberExpressionAst(Type type, st
return result;
}

#if !PSV3
#if !(PSV3||PSV4)
/// <summary>
/// Checks whether a class with the name name exists in the script that contains ast
/// </summary>
Expand Down
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,33 @@ Note: the PSScriptAnalyzer Chocolatey package is provided and supported by the c
* Building
You can either build using the `Visual Studio` solution `PSScriptAnalyzer.sln` or build using `PowerShell` specifically for your platform as follows:
* Windows PowerShell version 5.0 and greater
* The default build is for PowerShell Core
```powershell
.\buildCoreClr.ps1 -Framework net451 -Configuration Release -Build
.\build.ps1
```
* Windows PowerShell version 5.0
```powershell
.\build.ps1 -Framework full -PSVersion 5 -Configuration Release
```
* Windows PowerShell version 4.0
```powershell
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV4Release -Build
.\build.ps1 -Framework full -PSVersion 4 -Configuration Release
```
* Windows PowerShell version 3.0
```powershell
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV3Release -Build
.\build.ps1 -Framework full -PSVersion 3 -Configuration Release
```
* PowerShell Core
```powershell
.\buildCoreClr.ps1 -Framework netstandard2.0 -Configuration Release -Build
.\buildCoreClr.ps1 -Framework core -Configuration Release -Build
```
* Build documentation
```powershell
.\build.ps1 -Documentation
```
* Build documenatation
* Build all versions (PowerShell v3, v4, v5, and Core) and documentation
```powershell
.\build.ps1 -BuildDocs
.\build.ps1 -All
```
* Import the module
```powershell
Expand All @@ -157,12 +165,25 @@ For adding/removing resource strings in the `*.resx` files, it is recommended to
#### Tests
Pester-based ScriptAnalyzer Tests are located in `path/to/PSScriptAnalyzer/Tests` folder.

* Ensure [Pester 4.3.1](https://www.powershellgallery.com/packages/Pester/4.3.1) is installed
* Copy `path/to/PSScriptAnalyzer/out/PSScriptAnalyzer` to a folder in `PSModulePath`
* Ensure [Pester 4.3.1](https://www.powershellgallery.com/packages/Pester/4.3.1) or higher is installed
* Ensure that the documentation has been built (`./build.ps1 -Documentation`)
* In the root folder of your local repository, run:
``` PowerShell
$testScripts = ".\Tests\Engine",".\Tests\Rules",".\Tests\Documentation"
Invoke-Pester -Script $testScripts
./build -Test
```

To retrieve the results of the run, you can use the tools which are part of the build module (`build.psm1`)

```powershell
Import-Module ./build.psm1
Get-TestResults
```

To retrieve only the errors, you can use the following:

```powershell
Import-Module ./build.psm1
Get-TestFailures
```

[Back to ToC](#table-of-contents)
Expand Down
2 changes: 1 addition & 1 deletion Rules/DscExamplesPresent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
}
}

#if !PSV3
#if !(PSV3||PSV4)

/// <summary>
/// AnalyzeDSCClass: Analyzes given DSC class
Expand Down
2 changes: 1 addition & 1 deletion Rules/DscTestsPresent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
}
}

#if !PSV3
#if !(PSV3||PSV4)

/// <summary>
/// AnalyzeDSCClass: Analyzes given DSC class
Expand Down
6 changes: 3 additions & 3 deletions Rules/ReturnCorrectTypesForDSCFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName

IEnumerable<Ast> functionDefinitionAsts = Helper.Instance.DscResourceFunctions(ast);

#if !PSV3
#if !(PSV3||PSV4)

IEnumerable<TypeDefinitionAst> classes = ast.FindAll(item =>
item is TypeDefinitionAst
Expand All @@ -46,7 +46,7 @@ item is TypeDefinitionAst
foreach (FunctionDefinitionAst func in functionDefinitionAsts)
{

#if PSV3
#if PSV3 || PSV4

List<Tuple<string, StatementAst>> outputTypes = FindPipelineOutput.OutputTypes(func);

Expand Down Expand Up @@ -93,7 +93,7 @@ item is TypeDefinitionAst
}
}

#if !PSV3
#if !(PSV3||PSV4)

/// <summary>
/// AnalyzeDSCClass: Analyzes given DSC Resource
Expand Down
4 changes: 2 additions & 2 deletions Rules/UseOutputTypeCorrectly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
#endif
public class UseOutputTypeCorrectly : SkipTypeDefinition, IScriptRule
{
#if !PSV3
#if !(PSV3||PSV4)

private IEnumerable<TypeDefinitionAst> _classes;

Expand All @@ -41,7 +41,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
DiagnosticRecords.Clear();
this.fileName = fileName;

#if !PSV3
#if !(PSV3||PSV4)

_classes = ast.FindAll(item => item is TypeDefinitionAst && ((item as TypeDefinitionAst).IsClass), true).Cast<TypeDefinitionAst>();

Expand Down
2 changes: 1 addition & 1 deletion Rules/UseStandardDSCFunctionsInResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

#if PSV3
#if (PSV3||PSV4)

return null;

Expand Down
4 changes: 2 additions & 2 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Describe "Test importing correct customized rules" {
It "will show the custom rules when given a glob" {
# needs fixing for Linux
$expectedNumRules = 4
if ($IsLinux -or $IsMacOS)
if ($IsLinux)
{
$expectedNumRules = 3
}
Expand All @@ -113,7 +113,7 @@ Describe "Test importing correct customized rules" {
It "will show the custom rules when given glob with recurse switch" {
# needs fixing for Linux
$expectedNumRules = 5
if ($IsLinux -or $IsMacOS)
if ($IsLinux)
{
$expectedNumRules = 4
}
Expand Down
Loading

0 comments on commit 1eb9a01

Please sign in to comment.