1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 - 2021, Thomas Lauf, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // https://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <cmake.h>
28 #include <commands.h>
29 #include <shared.h>
30 #include <format.h>
31 #include <timew.h>
32 #include <iostream>
33 #include <sstream>
34 #include <FS.h>
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 // Given a partial match for an extension script name, find the full patch of
38 // the extension it may match.
findExtension(const Extensions & extensions,const std::string & partial)39 static std::string findExtension (
40   const Extensions& extensions,
41   const std::string& partial)
42 {
43   auto scripts = extensions.all ();
44   std::vector <std::string> options;
45   for (auto& script : scripts)
46   {
47     options.push_back (Path (script).name ());
48   }
49 
50   std::vector <std::string> matches;
51   autoComplete (partial, options, matches);
52 
53   if (matches.empty ())
54   {
55     throw format ("The report '{1}' is not recognized.", partial);
56   }
57 
58   if (matches.size () > 1)
59   {
60     throw format ("The report '{1}' is ambiguous, and could mean one of: {2}",
61                   partial,
62                   join (", ", matches));
63   }
64 
65   // Now map matches[0] back to it's corresponding option.
66   for (auto& script : scripts)
67   {
68     if (Path (script).name () == matches[0])
69     {
70       return script;
71     }
72   }
73 
74   return "";
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
CmdReport(const CLI & cli,Rules & rules,Database & database,const Extensions & extensions)78 int CmdReport (
79   const CLI& cli,
80   Rules& rules,
81   Database& database,
82   const Extensions& extensions)
83 {
84   std::string script;
85   for (auto& arg : cli._args)
86     if (arg.hasTag ("EXT"))
87       script = findExtension (extensions, arg.attribute ("canonical"));
88 
89   if (script.empty ())
90     throw std::string ("Specify which report to run.");
91 
92   // Compose Header info.
93   auto filter = cli.getFilter ();
94   auto tracked = getTracked (database, rules, filter);
95 
96   rules.set ("temp.report.start", filter.is_started () ? filter.start.toISO () : "");
97   rules.set ("temp.report.end",   filter.is_ended ()   ? filter.end.toISO ()   : "");
98   rules.set ("temp.report.tags", joinQuotedIfNeeded (",", filter.tags ()));
99   rules.set ("temp.version", VERSION);
100 
101   std::stringstream header;
102   for (auto& name : rules.all ())
103     header << name << ": " << rules.get (name) << '\n';
104 
105   // Get the data.
106   auto input = header.str ()
107              + '\n'
108              + jsonFromIntervals (tracked);
109 
110   // Run the extensions.
111   std::vector <std::string> output;
112   int rc = extensions.callExtension (script, split (input, '\n'), output);
113   if (rc != 0 && output.size () == 0)
114   {
115     throw format ("'{1}' returned {2} without producing output.", script, rc);
116   }
117 
118   // Display the output.
119   for (auto& line : output)
120     std::cout << line << '\n';
121 
122   return 0;
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126