<commit_before>import ProjectAnalyzer from './ProjectAnalyzer';
import { ProjectSettings } from '../interfaces/settings'

export default class ContextAssembler {
    public projectSettings: ProjectSettings = null;
    public orderedContextObjects: Array<Object> = [];

    constructor(public workingDir: string) {};
    
    public assemble() {
        return ProjectAnalyzer.gatherProjSettings(this.workingDir).then(settings => {
            this.projectSettings = settings;
            return settings;
        }).then(settings => {
            let proms = [ProjectAnalyzer.gatherContext(this.workingDir)];
            for (let inheritedContextPath of settings.inheritanceChain) {
                let path = `${settings.inheritanceRoot}/${inheritedContextPath}`;
                proms.push(ProjectAnalyzer.gatherContext(path));
            }
            return Promise.all(proms)
        }).then(contexts => {
            this.orderedContextObjects = this.orderedContextObjects.concat(contexts);
            return this
        })
    };
    
}

let f = new ContextAssembler("/Users/emurray/WebstormProjects/lede/spec/stubs/test-proj");
f.assemble();<commit_msg>Changed context assembler to build inherited contexts as properties of main context<commit_after>import ProjectAnalyzer from './ProjectAnalyzer';
import { ProjectSettings } from '../interfaces/settings';

class NamespaceErr extends Error {
    name: string;
    message: string;
    stack: any;

    constructor(msg) {
        super();
        this.name = "Namespace Error";
        this.message = msg;
        this.stack = (new Error()).stack;
    }
}

export default class ContextAssembler {
    public projectSettings: ProjectSettings = null;
    public context: Object;
    public includePaths: Array<Array<string>>;

    constructor(public workingDir: string) {};
    
    public assemble() {
        return ProjectAnalyzer.gatherProjSettings(this.workingDir).then(settings => {
            this.projectSettings = settings;
            return settings;
        }).then(settings => {
            let proms = [ProjectAnalyzer.gatherContext(this.workingDir)];
            for (let templateName of settings.inheritanceChain) {
                let path = `${settings.inheritanceRoot}/${templateName}`;
                proms.push(ProjectAnalyzer.gatherContext(path).then( ctx => [templateName, ctx] ));
            }
            return Promise.all(proms)
        }).then(contexts => {
            let mainCtx = contexts.shift();
            this.includePaths = [[`${this.workingDir}`]];
            for (let [contextName, context] of contexts) {
                if (!mainCtx.hasOwnProperty(contextName)) {
                    mainCtx[contextName] = context;
                } else {
                    throw new NamespaceErr(`Cannot load template ${contextName} because a property of that name already exists on base context.`)
                }
                let newIncludePaths = [];
                for (let paths of this.includePaths) {
                    for (let path of paths) {
                        newIncludePaths.push(path);
                    }
                }
                newIncludePaths.push(`${this.projectSettings.inheritanceRoot}/${contextName}`);
                this.includePaths.push(newIncludePaths);
            }
            this.context = mainCtx;
            return this;
        })
    };
}
{'fraction_non_alphanumeric': 0.10613207547169812, 'fraction_numerical': 0.0, 'mean_word_length': 2.2406876790830945, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2240798', 'n_tokens_mistral': 855, 'n_tokens_neox': 841, 'n_words': 189}