Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #25 from JoshuaKGoldberg/settings-refactor
Browse files Browse the repository at this point in the history
Settings refactor
  • Loading branch information
Josh Goldberg committed Apr 26, 2016
2 parents 204e7ae + 2aee235 commit 6a076af
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 83 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TSLint for MSBuild

An MSBuild wrapper around Palantir's wonderful [tslint](https://github.com/palantir/tslint). Get it at [nuget.org](https://www.nuget.org/packages/TSLint.MSBuild/).
An MSBuild target for linting TypeScript code using [TSLint](https://github.com/palantir/tslint). Get it at [nuget.org](https://www.nuget.org/packages/TSLint.MSBuild/).

## Usage

Expand All @@ -14,11 +14,13 @@ A .js runner file then takes in the path to that file list, scans for `tslint.js

The following properties may be overidden via your targets:
* **TSLintDeleteFileListFile** - Whether to delete the file list file when done. Defaults to `true`.
* **TSLintExclude** - A JavaScript RegExp literal of matching file names to exclude. Defaults to `"^$"` (none).
* **TSLintFilesRootDir** - A root directory to work within. Defaults to `$(MSBuildProjectDirectory)`.
* **TSLintFileListDir** - The directory to put the file list in. Defaults to `$(IntermediateOutDir)`.
* **TSLintFileListName** - The name of the file list file. Defaults to `TSLintFileList.txt-$(MSBuildProjectName)`.
* **TSLintNodeExe**: A node executable to execute the runner script. Defaults to the `tools\node-5.9.0.exe` in the package.
* **TSLintRunnerScript** - The .js file to take in `TSLintFileListFile`. Defaults to the `tools\runner.js` in the package.
* **TSLintFilesRootDir** - A root directory to work within. Defaults to `$(MSBuildProjectDirectory)`.


### tslint.json

Expand Down
6 changes: 3 additions & 3 deletions TSLint.MSBuild.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>TSLint.MSBuild</id>
<version>0.1.3</version>
<version>0.2.0</version>
<authors>palantir, joshuakgoldberg</authors>
<owners>joshuakgoldberg</owners>
<licenseUrl>https://github.com/joshuakgoldberg/TSLint.MSBuild/blob/master/LICENSE.md</licenseUrl>
<projectUrl>https://github.com/joshuakgoldberg/TSLint.MSBuild</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>
An MSBuild target for Palantir's wonderful TSLint.
An MSBuild target for linting TypeScript code using TSLint.
</description>
<tags>tslint, msbuild</tags>
<dependencies>
<dependency id="tslint" version="3.5.2" />
<dependency id="tslint" version="3.7.0" />
</dependencies>
</metadata>
<files>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tslint-msbuild",
"version": "0.1.3",
"description": " An MSBuild target for Palantir's wonderful TSLint.",
"version": "0.2.0",
"description": " An MSBuild target for linting TypeScript code using TSLint.",
"author": "Joshua K Goldberg",
"license": "MIT",
"repository": {
Expand Down
105 changes: 105 additions & 0 deletions src/ArgumentsCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
namespace TSLint.MSBuild {
"use strict";

/**
* A parser and storer for command-line arguments to TSLint.MSBuild.
*/
export class ArgumentsCollection {
/**
* Value setters for arguments, keyed by alias.
*/
private static valueSetters: { [i: string]: (value: string) => void } = {
"exclude": ArgumentsCollection.prototype.setExclude,
"files-root-dir": ArgumentsCollection.prototype.setFilesRootDir,
"file-list-file": ArgumentsCollection.prototype.setFileListFile
};

/**
* A glob path to exclude from linting.
*
* @alias exclude
*/
private exclude: RegExp;

/**
* A root directory to work within.
*
* @alias files-root-dir
*/
private filesRootDir: string;

/**
* The path to the file listing files to be linted.
*
* @alias file-list-file
*/
private fileListFile: string;

/**
* Initializes a new instance of the ArgumentsCollection class.
*
* @param inputs Raw command-line input.
*/
constructor(inputs: string[]) {
for (let i: number = 0; i < inputs.length; i += 2) {
const alias = inputs[i].replace("-", "");
const value = inputs[i + 1];

if (!ArgumentsCollection.valueSetters.hasOwnProperty(alias)) {
throw new Error(`Unknown TSLint.MSBuild argument: '${inputs[i]}' '${value}'`);
}

console.log(`Setting '${alias}' to '${value}'.`);
ArgumentsCollection.valueSetters[alias].call(this, value);
}
}

/**
* @returns The FilesRootDir argument.
*/
public getFilesRootDir(): string {
return this.filesRootDir;
}

/**
* @returns The FileListFile argument.
*/
public getFileListFile(): string {
return this.fileListFile;
}

/**
* @returns The Exclude argument.
*/
public getExclude(): RegExp {
return this.exclude;
}

/**
* Sets the FilesRootDir argument.
*
* @param value A new FilesRootDir value.
*/
private setFilesRootDir(value: string): void {
this.filesRootDir = value;
}

/**
* Sets the FileListFile argument.
*
* @param value A new FileListFile value.
*/
private setFileListFile(value: string): void {
this.fileListFile = value;
}

/**
* Sets the Exclude argument.
*
* @param value A new Exclude value.
*/
private setExclude(value: string): void {
this.exclude = new RegExp(value || "^$", "i");
}
}
}
37 changes: 0 additions & 37 deletions src/ConfigLoader.ts

This file was deleted.

36 changes: 25 additions & 11 deletions src/Folder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/// <reference path="../typings/main/ambient/node/index.d.ts" />
/// <reference path="ArgumentsCollection.ts" />
/// <reference path="WaitLock.ts" />
/// <reference path="./ConfigLoader.ts" />

namespace TSLint.MSBuild {
"use strict";

let fs = require("fs"),
path = require("path");

/**
* A representation of a directory with files and optionally a tsconfig.json.
*/
Expand Down Expand Up @@ -35,9 +36,8 @@ namespace TSLint.MSBuild {
* Initializes a new instance of the Folder class.
*
* @param path The path to this folder.
* @param configLoader The configuration loader.
*/
constructor(path: string, private configLoader: ConfigLoader) {
constructor(path: string) {
this.path = path;
}

Expand Down Expand Up @@ -88,18 +88,22 @@ namespace TSLint.MSBuild {
*/
public loadTSLintConfig(): Promise<boolean> {
this.loadWaiter.markActionStart();
return this.configLoader
.readJSONConfig(path.join(this.path, "tslint.json"))
.then((config) => {

return new Promise(resolve => {
fs.readFile(path.join(this.path, "tslint.json"), (error, result) => {
if (error) {
this.setTSLintConfig(undefined);
resolve(false);
return;
}

this.setTSLintConfig({
formatter: "json",
configuration: config
configuration: this.sanitizeFileContents(result)
});
return true;
})
.catch((error) => {
this.setTSLintConfig(undefined);
resolve(true);
});
});
}

/**
Expand All @@ -112,5 +116,15 @@ namespace TSLint.MSBuild {
return this.loadWaiter.addCallback(() => resolve(this));
});
}

/**
* Sanitizes a file's contents in case of an odd BOM.
*
* @param raw Raw contents of a file.
* @returns The BOM-sanitized equivalent text.
*/
private sanitizeFileContents(raw: any): string {
return JSON.parse(raw.toString().replace(/^\uFEFF/, ""));
}
}
}
17 changes: 9 additions & 8 deletions src/FolderCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="../typings/main/ambient/node/index.d.ts" />
/// <reference path="ArgumentsCollection.ts" />
/// <reference path="Folder.ts" />

namespace TSLint.MSBuild {
Expand All @@ -9,9 +10,9 @@ namespace TSLint.MSBuild {
*/
export class FolderCollection {
/**
* The root directory to look at files under.
* Parsed arguments to the program.
*/
private rootDirectory: string;
private argumentsCollection: ArgumentsCollection;

/**
* Known folders that have been added.
Expand All @@ -21,11 +22,10 @@ namespace TSLint.MSBuild {
/**
* Initializes a new instance of the LintRunner class.
*
* @param rootDirectory The root directory to look at files under.
* @param ConfigLoader The configuration loader.
* @param argumentsCollection Parsed arguments to the program.
*/
constructor(rootDirectory: string, private configLoader: ConfigLoader) {
this.rootDirectory = rootDirectory;
constructor(argumentsCollection: ArgumentsCollection) {
this.argumentsCollection = argumentsCollection;
}

/**
Expand Down Expand Up @@ -55,6 +55,7 @@ namespace TSLint.MSBuild {
* Adds a file path and its containing folder path.
*
* @param filePath A path to a file.
* @returns A promise of the file being added.
*/
private addFilePath(filePath: string): Promise<void> {
return this
Expand All @@ -75,7 +76,7 @@ namespace TSLint.MSBuild {
return new Promise(resolve => resolve(folder));
}

folder = this.folders[folderPath] = new Folder(folderPath, this.configLoader);
folder = this.folders[folderPath] = new Folder(folderPath);

return folder
.loadTSLintConfig()
Expand Down Expand Up @@ -113,7 +114,7 @@ namespace TSLint.MSBuild {
* @todo Should this reject instead of resolve with undefined?
*/
private checkFolderParent(folderPath: string): Promise<Folder> {
if (folderPath.length < this.rootDirectory.length) {
if (folderPath.length < this.argumentsCollection.getFilesRootDir().length) {
return new Promise(resolve => resolve(undefined));
}

Expand Down
14 changes: 4 additions & 10 deletions src/LintRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="../typings/main/ambient/node/index.d.ts" />
/// <reference path="ArgumentsCollection.ts" />
/// <reference path="Folder.ts" />
/// <reference path="FolderCollection.ts" />
/// <reference path="TSLintSearcher.ts" />
Expand All @@ -12,11 +13,6 @@ namespace TSLint.MSBuild {
* Driver for running TSLint on a number of files.
*/
export class LintRunner {
/**
* The root directory to look at files under.
*/
private rootDirectory: string;

/**
* Folders generated from individual file paths.
*/
Expand All @@ -35,12 +31,10 @@ namespace TSLint.MSBuild {
/**
* Initializes a new instance of the LintRunner class.
*
* @param rootDirectory The root directory to look at files under.
* @param configLoader The configuration loader.
* @param argumentsCollection Parsed arguments to the program.
*/
constructor(rootDirectory: string, private configLoader: ConfigLoader) {
this.rootDirectory = rootDirectory;
this.folders = new FolderCollection(rootDirectory, this.configLoader);
constructor(argumentsCollection: ArgumentsCollection) {
this.folders = new FolderCollection(argumentsCollection);
this.tsLintSearcher = new TSLintSearcher();
this.tsLint = require(this.tsLintSearcher.resolve());
}
Expand Down
5 changes: 3 additions & 2 deletions src/TSLint.MSBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
Name="TSLint">
<PropertyGroup>
<TSLintDeleteFileListFile Condition="'$(TSLintDeleteFileListFile)' == ''">true</TSLintDeleteFileListFile>
<TSLintExclude Condition="'$(TSLintExclude)' == ''">^$</TSLintExclude>
<TSLintFilesRootDir Condition="'$(TSLintFilesRootDir)' == ''">$(MSBuildProjectDirectory)</TSLintFilesRootDir>
<TSLintFileListDir Condition="'$(TSLintFileListDir)' == ''">$(IntermediateOutDir)</TSLintFileListDir>
<TSLintFileListName Condition="'$(TSLintFileListName)' == ''">TSLintFileList-$(MSBuildProjectName).txt</TSLintFileListName>
<TSLintFileListFile>$(TSLintFileListDir)$(TSLintFileListName)</TSLintFileListFile>
<TSLintNodeExe Condition="'$(TSLintNodeExe)' == ''">$([System.IO.Path]::GetFullPath("$(MSBuildThisFileDirectory)\..\tools\node-5.9.0.exe"))</TSLintNodeExe>
<TSLintRunnerScript Condition="'$(TSLintRunnerScript)' == ''">$([System.IO.Path]::GetFullPath("$(MSBuildThisFileDirectory)\..\tools\runner.js"))</TSLintRunnerScript>
<TSLintFilesRootDir Condition="'$(TSLintFilesRootDir)' == ''">$(MSBuildProjectDirectory)</TSLintFilesRootDir>
</PropertyGroup>

<ItemGroup>
Expand All @@ -32,7 +33,7 @@

<!-- Run TSLint via the runner -->
<Exec
Command="&quot;$(TSLintNodeExe)&quot; --harmony &quot;$(TSLintRunnerScript)&quot; &quot;$(TSLintFilesRootDir)&quot; &quot;$(TSLintFileListFile)&quot;"
Command="&quot;$(TSLintNodeExe)&quot; --harmony &quot;$(TSLintRunnerScript)&quot; -exclude &quot;$(TSLintExclude)&quot; -files-root-dir &quot;$(TSLintFilesRootDir)&quot; -file-list-file &quot;$(TSLintFileListFile)&quot;"
IgnoreExitCode="true" />

<!-- Clean up our compiled file list when done -->
Expand Down
Loading

0 comments on commit 6a076af

Please sign in to comment.