<commit_before>/*
 * grunt-yaml-to-swagger
 * 
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function (grunt) {

    // Please see the Grunt documentation for more information regarding task
    // creation: http://gruntjs.com/creating-tasks
    Array.prototype.getUnique = function () {
        var u = {}, a = [];
        for (var i = 0, l = this.length; i < l; ++i) {
            if (u.hasOwnProperty(this[i])) {
                continue;
            }
            a.push(this[i]);
            u[this[i]] = 1;
        }
        return a;
    }

    var primitives = [
        "array",
        "boolean",
        "integer",
        "number",
        "null",
        "object",
        "string"
    ];

    function contains(a, obj) {
        var i = a.length;
        while (i--) {
            if (a[i] === obj) {
                return true;
            }
        }
        return false;
    }

    function extend(target) {
        var sources = [].slice.call(arguments, 1);
        sources.forEach(function (source) {
            for (var prop in source) {
                target[prop] = source[prop];
            }
        });
        return target;
    }

    function isEmpty(obj) {
        for (var prop in obj) {
            if (obj.hasOwnProperty(prop))
                return false;
        }

        return true;
    }

    function execSync(cmd, args, callback) {
        var options = {
            // The command to execute. It should be in the system path.
            cmd: cmd,
            args: args
        };
        grunt.util.spawn(options, callback)
    }

    function parseFiles(files, options, callback) {
        var fs = require('fs');
        var path = require('path').resolve(options.route_path + '/');
        var working = false;
        var interval = setInterval(function () {
            if (files.length > 0) {
                if (working == false) {
                    working = true;
                    var file = files[0];
                    var file_path = require('path').resolve(path + '/' + file);
                    try {
                        var route_definitions = require('yamljs').load(file_path);
                    } catch (e) {
                        console.log("error");
                        grunt.fatal(e.message);
                    }

                    var base_filename = file.split(".yml")[0];
                    var outputFilename = options.output_docs_path + "/" + base_filename + ".json";

                    var returnData = {};
                    //console.log(JSON.stringify(route_definitions));
                    var models = [];
                    for (var j = 0; j < route_definitions.apis.length; j++) {
                        var operations = route_definitions.apis[j].operations;
                        for (var i = 0; i < operations.length; i++) {
                            if (!contains(primitives, operations[i].type)) {
                                models.push(operations[i].type);
                            }

                        }
                    }
                    models = models.getUnique();
                    parseModels(models, options, function (data) {
                        route_definitions.models = data;

                        var pretty_route_definitions = JSON.stringify(route_definitions, undefined, 2);

                        var matches = pretty_route_definitions.match(/:(\w)+/g);
                        if (matches != null && matches.length > 0) {
                            for (var replace = 0; replace < matches.length; replace++) {
                                var field = matches[replace].split(":").join("");
                                pretty_route_definitions = pretty_route_definitions.split(matches[replace]).join("{" + field + "}");
                            }
                        }

                        fs.writeFileSync(outputFilename, pretty_route_definitions);
                        grunt.log.ok(base_filename + ".json created");
                        files.splice(0, 1);
                        working = false;
                    });

                }
            } else {
                clearInterval(interval);
                callback();
            }
        }, 100);
    }

    function parseModels(models, options, callback) {
        var fs = require('fs');
        var returnData = {};
        var working = false;
        var interval = setInterval(function () {
            if (models.length > 0) {
                if (working == false) {
                    working = true;
                    var args = ['schema', require("path").resolve(options.models_path + '/' + models[0] + '.ts')];
                    execSync('typson', args, function (error, result, code) {
                        if (error == null) {
                            var schemaJson = JSON.parse(result);
                            schemaJson.basePath = options.base_url_ws;
                            var result = JSON.stringify(schemaJson);
                            var pattern = new RegExp('\[MockType=(\w)+\]', "g");
                            var matches = result.match(/\[MockType=(\w)+\]/g);
                            if (matches != null && matches.length > 0) {
                                for (var replace = 0; replace < matches.length; replace++) {
                                    result = result.split(matches[replace]).join("");
                                }
                            }
                            returnData = extend(returnData, JSON.parse(result));
                            models.splice(0, 1);
                            working = false;
                        } else {
                            grunt.log.error('typson ' + args.join(" "));
                            grunt.fatal(error);
                        }

                    });<commit_msg>Adding basePath to route by config file<commit_after>/*
 * grunt-yaml-to-swagger
 * 
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function (grunt) {

    // Please see the Grunt documentation for more information regarding task
    // creation: http://gruntjs.com/creating-tasks
    Array.prototype.getUnique = function () {
        var u = {}, a = [];
        for (var i = 0, l = this.length; i < l; ++i) {
            if (u.hasOwnProperty(this[i])) {
                continue;
            }
            a.push(this[i]);
            u[this[i]] = 1;
        }
        return a;
    }

    var primitives = [
        "array",
        "boolean",
        "integer",
        "number",
        "null",
        "object",
        "string"
    ];

    function contains(a, obj) {
        var i = a.length;
        while (i--) {
            if (a[i] === obj) {
                return true;
            }
        }
        return false;
    }

    function extend(target) {
        var sources = [].slice.call(arguments, 1);
        sources.forEach(function (source) {
            for (var prop in source) {
                target[prop] = source[prop];
            }
        });
        return target;
    }

    function isEmpty(obj) {
        for (var prop in obj) {
            if (obj.hasOwnProperty(prop))
                return false;
        }

        return true;
    }

    function execSync(cmd, args, callback) {
        var options = {
            // The command to execute. It should be in the system path.
            cmd: cmd,
            args: args
        };
        grunt.util.spawn(options, callback)
    }

    function parseFiles(files, options, callback) {
        var fs = require('fs');
        var path = require('path').resolve(options.route_path + '/');
        var working = false;
        var interval = setInterval(function () {
            if (files.length > 0) {
                if (working == false) {
                    working = true;
                    var file = files[0];
                    var file_path = require('path').resolve(path + '/' + file);
                    try {
                        var route_definitions = require('yamljs').load(file_path);
                    } catch (e) {
                        console.log("error");
                        grunt.fatal(e.message);
                    }

                    var base_filename = file.split(".yml")[0];
                    var outputFilename = options.output_docs_path + "/" + base_filename + ".json";

                    var returnData = {};
                    //console.log(JSON.stringify(route_definitions));
                    var models = [];
                    for (var j = 0; j < route_definitions.apis.length; j++) {
                        var operations = route_definitions.apis[j].operations;
                        for (var i = 0; i < operations.length; i++) {
                            if (!contains(primitives, operations[i].type)) {
                                models.push(operations[i].type);
                            }

                        }
                    }
                    models = models.getUnique();
                    parseModels(models, options, function (data) {
                        route_definitions.models = data;

                        var pretty_route_definitions = JSON.stringify(route_definitions, undefined, 2);

                        var matches = pretty_route_definitions.match(/:(\w)+/g);
                        if (matches != null && matches.length > 0) {
                            for (var replace = 0; replace < matches.length; replace++) {
                                var field = matches[replace].split(":").join("");
                                pretty_route_definitions = pretty_route_definitions.split(matches[replace]).join("{" + field + "}");
                            }
                        }

                        fs.writeFileSync(outputFilename, pretty_route_definitions);
                        grunt.log.ok(base_filename + ".json created");
                        files.splice(0, 1);
                        working = false;
                    });

                }
            } else {
                clearInterval(interval);
                callback();
            }
        }, 100);
    }

    function parseModels(models, options, callback) {
        var fs = require('fs');
        var returnData = {};
        var working = false;
        var interval = setInterval(function () {
            if (models.length > 0) {
                if (working == false) {
                    working = true;
                    var args = ['schema', require("path").resolve(options.models_path + '/' + models[0] + '.ts')];
                    execSync('typson', args, function (error, result, code) {
                        if (error == null) {
                            var schemaJson = JSON.parse(result);
                            schemaJson.basePath = options.base_url_ws;
                            var result = JSON.stringify(schemaJson);
                            console.log(result);
                            var pattern = new RegExp('\[MockType=(\w)+\]', "g");
                            var matches = result.match(/\[MockType=(\w)+\]/g);
                            if (matches != null && matches.length > 0) {
                                for (var replace = 0; replace < matches.length; replace++) {
                                    result = result.split(matches[replace]).join("");
                                }
                            }
                            returnData = extend(returnData, JSON.parse(result));
                            models.splice(0, 1);
                            working = false;
                        } else {
                            grunt.log.error('typson ' + args.join(" "));
                            grunt.fatal(error);
                        }

                    });
{'fraction_non_alphanumeric': 0.1177749895441238, 'fraction_numerical': 0.004349644500209118, 'mean_word_length': 1.1338568623951455, 'pattern_counts': {'":': 2, '<': 15, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1518771', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2846, 'n_words': 659}