1 /**
2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module dextool.plugin.runner;
11 
12 import logger = std.experimental.logger;
13 
14 import dextool.type : ExitStatusType, Path, AbsolutePath;
15 
16 ExitStatusType runPlugin(string[] args) @safe {
17     import std.stdio : writeln, writefln;
18     import dextool.compilation_db : CompileCommandDB, fromArgCompileDb;
19     import dextool.plugin.analyze.raw_config;
20     import dextool.plugin.analyze.analyze : AnalyzeBuilder, AnalyzeResults, doAnalyze;
21 
22     RawConfiguration pargs;
23     pargs.parse(args);
24 
25     // the dextool plugin architecture requires that two lines are printed upon
26     // request by the main function.
27     //  - a name of the plugin.
28     //  - a oneliner description.
29     if (pargs.shortPluginHelp) {
30         writeln("analyze");
31         writeln("static code analysis of c/c++ source code");
32         return ExitStatusType.Ok;
33     } else if (pargs.errorHelp) {
34         pargs.printHelp;
35         return ExitStatusType.Errors;
36     } else if (pargs.help) {
37         pargs.printHelp;
38         return ExitStatusType.Ok;
39     }
40 
41     CompileCommandDB compile_db;
42     if (pargs.compileDb.length != 0) {
43         compile_db = () @trusted { return pargs.compileDb.fromArgCompileDb; }();
44     }
45 
46     // dfmt off
47     auto analyze_builder = AnalyzeBuilder.make
48         .mcCabe(pargs.mccabe);
49     auto analyze_results = AnalyzeResults.make
50         .mcCabe(pargs.mccabe, pargs.mccabeThreshold)
51         .json(pargs.outputJson)
52         .stdout(pargs.outputStdout)
53         .outputDirectory(pargs.outdir)
54         .finalize;
55     // dfmt on
56 
57     doAnalyze(analyze_builder, analyze_results, pargs.cflags, pargs.files,
58             compile_db, AbsolutePath(Path(pargs.restrictDir)), pargs.workerThreads);
59 
60     analyze_results.dumpResult;
61 
62     return ExitStatusType.Ok;
63 }