Simplify by removing diff feature. Now just writes each template as an independent copy.

This commit is contained in:
SteveSandersonMS
2016-04-28 14:34:47 +01:00
parent 1830c20212
commit fc705e8bf8
4 changed files with 6 additions and 117 deletions

View File

@@ -1,10 +1,8 @@
import * as yeoman from 'yeoman-generator';
import * as glob from 'glob';
import * as gitignore from 'gitignore-parser';
import * as fs from 'fs';
import * as path from 'path';
import * as _ from 'lodash';
import * as diff from 'diff';
import * as mkdirp from 'mkdirp';
import * as rimraf from 'rimraf';
@@ -33,63 +31,18 @@ function listFilesExcludingGitignored(root: string): string[] {
.filter(fn => gitignoreEvaluator.accepts(fn));
}
function writeCommonFiles(outDir: string) {
let filesByTemplate = _.mapValues(templates, listFilesExcludingGitignored);
let commonFiles = _.intersection.apply(_, _.values(filesByTemplate));
commonFiles.forEach(fn => {
let templateRoots = _.values(templates);
let origContent = fs.readFileSync(path.join(templateRoots[0], fn));
if (isTextFile(fn)) {
// For text files, we copy the portion that's common to all the templates
let commonText = origContent.toString('utf8');
templateRoots.slice(1).forEach(otherTemplateRoot => {
let otherTemplateContent = fs.readFileSync(path.join(otherTemplateRoot, fn), 'utf8');
commonText = diff.diffLines(commonText, otherTemplateContent)
.filter(c => !(c.added || c.removed))
.map(c => c.value)
.join('');
});
writeFileEnsuringDirExists(outDir, fn, commonText);
} else {
// For binary (or maybe-binary) files, we only consider them common if they are identical across all templates
let isIdenticalEverywhere = !templateRoots.slice(1).some(otherTemplateRoot => {
return !fs.readFileSync(path.join(otherTemplateRoot, fn)).equals(origContent);
});
if (isIdenticalEverywhere) {
writeFileEnsuringDirExists(outDir, fn, origContent);
}
}
});
}
function writeDiffsForTemplate(sourceRoot: string, destRoot: string, commonRoot: string) {
function writeTemplate(sourceRoot: string, destRoot: string) {
listFilesExcludingGitignored(sourceRoot).forEach(fn => {
const commonFn = path.join(commonRoot, fn);
const sourceContent = fs.readFileSync(path.join(sourceRoot, fn));
if (!fs.existsSync(commonFn)) {
// This file is unique to this template - just copy as-is
writeFileEnsuringDirExists(destRoot, fn, sourceContent);
} else {
let commonText = fs.readFileSync(commonFn, 'utf8');
let sourceText = sourceContent.toString('utf8');
if (commonText !== sourceText) {
// Write a diff vs the common version of this file
let fileDiff = diff.createPatch(fn, commonText, sourceText, null, null);
writeFileEnsuringDirExists(destRoot, fn + '.patch', fileDiff);
}
}
});
const sourceContent = fs.readFileSync(path.join(sourceRoot, fn));
writeFileEnsuringDirExists(destRoot, fn, sourceContent);
});
}
const outputRoot = './generator-aspnet-spa';
const commonRoot = path.join(outputRoot, 'templates/common');
rimraf.sync(outputRoot);
writeCommonFiles(commonRoot);
_.forEach(templates, (templateRootDir, templateName) => {
writeDiffsForTemplate(templateRootDir, path.join(outputRoot, 'templates', templateName), commonRoot);
const outputDir = path.join(outputRoot, 'templates', templateName);
writeTemplate(templateRootDir, outputDir);
});

View File

@@ -20,9 +20,6 @@
"lodash/lodash.d.ts": {
"commit": "544a35a10866b32afda9c7f029c0764558563f4f"
},
"diff/diff.d.ts": {
"commit": "544a35a10866b32afda9c7f029c0764558563f4f"
},
"mkdirp/mkdirp.d.ts": {
"commit": "544a35a10866b32afda9c7f029c0764558563f4f"
},

View File

@@ -1,60 +0,0 @@
// Type definitions for diff
// Project: https://github.com/kpdecker/jsdiff
// Definitions by: vvakame <https://github.com/vvakame/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace JsDiff {
interface IDiffResult {
value: string;
count?: number;
added?: boolean;
removed?: boolean;
}
interface IBestPath {
newPos: number;
componenets: IDiffResult[];
}
class Diff {
ignoreWhitespace:boolean;
constructor(ignoreWhitespace?:boolean);
diff(oldString:string, newString:string):IDiffResult[];
pushComponent(components:IDiffResult[], value:string, added:boolean, removed:boolean):void;
extractCommon(basePath:IBestPath, newString:string, oldString:string, diagonalPath:number):number;
equals(left:string, right:string):boolean;
join(left:string, right:string):string;
tokenize(value:string):any; // return types are string or string[]
}
function diffChars(oldStr:string, newStr:string):IDiffResult[];
function diffWords(oldStr:string, newStr:string):IDiffResult[];
function diffWordsWithSpace(oldStr:string, newStr:string):IDiffResult[];
function diffJson(oldObj: Object, newObj: Object): IDiffResult[];
function diffLines(oldStr:string, newStr:string):IDiffResult[];
function diffCss(oldStr:string, newStr:string):IDiffResult[];
function createPatch(fileName:string, oldStr:string, newStr:string, oldHeader:string, newHeader:string):string;
function applyPatch(oldStr:string, uniDiff:string):string;
function convertChangesToXML(changes:IDiffResult[]):string;
function convertChangesToDMP(changes:IDiffResult[]):{0: number; 1:string;}[];
}
declare module "diff" {
export = JsDiff;
}

View File

@@ -3,6 +3,5 @@
/// <reference path="glob/glob.d.ts" />
/// <reference path="minimatch/minimatch.d.ts" />
/// <reference path="lodash/lodash.d.ts" />
/// <reference path="diff/diff.d.ts" />
/// <reference path="mkdirp/mkdirp.d.ts" />
/// <reference path="rimraf/rimraf.d.ts" />