add map options
This commit is contained in:
parent
92d4caf5c0
commit
0e9aa3b023
4 changed files with 71 additions and 9 deletions
|
|
@ -2,12 +2,19 @@
|
||||||
#define MINIFY_HPP
|
#define MINIFY_HPP
|
||||||
|
|
||||||
#include "struc.hpp"
|
#include "struc.hpp"
|
||||||
|
#include "processing.hpp"
|
||||||
|
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void minify_var(_obj* in, std::regex const& exclude);
|
std::string gen_minmap(strmap_t const& map, std::string const& prefix);
|
||||||
void minify_fct(_obj* in, std::regex const& exclude);
|
void read_minmap(std::string const& filepath, strmap_t* varmap, strmap_t* fctmap);
|
||||||
|
|
||||||
|
bool r_replace_fct(_obj* in, strmap_t* fctmap);
|
||||||
|
bool r_replace_var(_obj* in, strmap_t* varmap);
|
||||||
|
|
||||||
|
strmap_t minify_var(_obj* in, std::regex const& exclude);
|
||||||
|
strmap_t minify_fct(_obj* in, std::regex const& exclude);
|
||||||
|
|
||||||
void delete_unused(_obj* in, std::regex const& var_exclude, std::regex const& fct_exclude);
|
void delete_unused(_obj* in, std::regex const& var_exclude, std::regex const& fct_exclude);
|
||||||
|
|
||||||
|
|
|
||||||
20
src/main.cpp
20
src/main.cpp
|
|
@ -194,22 +194,28 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
// processing before output
|
// processing before output
|
||||||
// minify
|
// minify
|
||||||
|
strmap_t varmap, fctmap;
|
||||||
if(options['m'])
|
if(options['m'])
|
||||||
{
|
{
|
||||||
opt_minify=true;
|
opt_minify=true;
|
||||||
minify_generic(sh);
|
minify_generic(sh);
|
||||||
}
|
}
|
||||||
if(options["minify-var"] && options["minify-fct"]) {
|
if(options['A']) {
|
||||||
|
read_minmap(options['A'].argument, &varmap, &fctmap);
|
||||||
|
recurse(r_replace_var, sh, &varmap);
|
||||||
|
recurse(r_replace_fct, sh, &fctmap);
|
||||||
|
}
|
||||||
|
else if(options["minify-var"] && options["minify-fct"]) {
|
||||||
// optimization: get everything in one go
|
// optimization: get everything in one go
|
||||||
allmaps_get(sh, re_var_exclude, re_fct_exclude, regex_null);
|
allmaps_get(sh, re_var_exclude, re_fct_exclude, regex_null);
|
||||||
minify_var( sh, re_var_exclude );
|
varmap = minify_var( sh, re_var_exclude );
|
||||||
minify_fct( sh, re_fct_exclude );
|
fctmap = minify_fct( sh, re_fct_exclude );
|
||||||
}
|
}
|
||||||
else if(options["minify-var"]) {
|
else if(options["minify-var"]) {
|
||||||
minify_var( sh, re_var_exclude );
|
varmap = minify_var( sh, re_var_exclude );
|
||||||
}
|
}
|
||||||
else if(options["minify-fct"]) {
|
else if(options["minify-fct"]) {
|
||||||
minify_fct( sh, re_fct_exclude );
|
fctmap = minify_fct( sh, re_fct_exclude );
|
||||||
}
|
}
|
||||||
// other processing
|
// other processing
|
||||||
if(options["unset-var"])
|
if(options["unset-var"])
|
||||||
|
|
@ -223,6 +229,10 @@ int main(int argc, char* argv[])
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if(options['P']) {
|
||||||
|
std::ofstream(options['P'].argument) << gen_minmap(varmap, "var") << gen_minmap(fctmap, "fct");
|
||||||
|
}
|
||||||
|
|
||||||
if(options['o']) // file output
|
if(options['o']) // file output
|
||||||
{
|
{
|
||||||
std::string destfile=options['o'];
|
std::string destfile=options['o'];
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "minify.hpp"
|
#include "minify.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "parse.hpp"
|
#include "parse.hpp"
|
||||||
#include "recursive.hpp"
|
#include "recursive.hpp"
|
||||||
|
|
@ -390,7 +391,7 @@ strmap_t gen_minimal_map(countmap_t const& vars, set_t const& excluded)
|
||||||
|
|
||||||
// calls
|
// calls
|
||||||
|
|
||||||
void minify_var(_obj* in, std::regex const& exclude)
|
strmap_t minify_var(_obj* in, std::regex const& exclude)
|
||||||
{
|
{
|
||||||
// countmap_t vars;
|
// countmap_t vars;
|
||||||
set_t excluded;
|
set_t excluded;
|
||||||
|
|
@ -405,9 +406,10 @@ void minify_var(_obj* in, std::regex const& exclude)
|
||||||
// perform replace
|
// perform replace
|
||||||
recurse(r_replace_var, in, &varmap);
|
recurse(r_replace_var, in, &varmap);
|
||||||
require_rescan_var();
|
require_rescan_var();
|
||||||
|
return varmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void minify_fct(_obj* in, std::regex const& exclude)
|
strmap_t minify_fct(_obj* in, std::regex const& exclude)
|
||||||
{
|
{
|
||||||
// countmap_t fcts, cmdmap;
|
// countmap_t fcts, cmdmap;
|
||||||
set_t excluded, unsets;
|
set_t excluded, unsets;
|
||||||
|
|
@ -428,6 +430,7 @@ void minify_fct(_obj* in, std::regex const& exclude)
|
||||||
recurse(r_replace_fct, in, &fctmap);
|
recurse(r_replace_fct, in, &fctmap);
|
||||||
require_rescan_fct();
|
require_rescan_fct();
|
||||||
require_rescan_cmd();
|
require_rescan_cmd();
|
||||||
|
return fctmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool delete_unused_fct(_obj* in, std::regex const& exclude)
|
bool delete_unused_fct(_obj* in, std::regex const& exclude)
|
||||||
|
|
@ -665,3 +668,31 @@ void minify_generic(_obj* in)
|
||||||
recurse(r_minify_backtick, in);
|
recurse(r_minify_backtick, in);
|
||||||
recurse(r_minify_useless_quotes, in);
|
recurse(r_minify_useless_quotes, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string gen_minmap(strmap_t const& map, std::string const& prefix)
|
||||||
|
{
|
||||||
|
std::string ret;
|
||||||
|
for(auto it: map) {
|
||||||
|
ret += strf("%s %s %s\n", prefix.c_str(), it.second.c_str(), it.first.c_str());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_minmap(std::string const& filepath, strmap_t* varmap, strmap_t* fctmap)
|
||||||
|
{
|
||||||
|
std::ifstream file(filepath);
|
||||||
|
std::string ln;
|
||||||
|
while(std::getline(file, ln)) {
|
||||||
|
size_t s1, s2, s3;
|
||||||
|
s1 = ln.find(' ');
|
||||||
|
s2 = ln.find(' ', s1+1);
|
||||||
|
s3 = ln.find(' ', s2+1);
|
||||||
|
std::string type = ln.substr(0, s1);
|
||||||
|
std::string from = ln.substr(s1+1, s2-s1-1);
|
||||||
|
std::string to = ln.substr(s2+1, s3-s2-1);
|
||||||
|
if(type == "var")
|
||||||
|
varmap->insert(std::make_pair(from, to));
|
||||||
|
else if(type == "fct")
|
||||||
|
fctmap->insert(std::make_pair(from, to));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ ztd::option_set options( {
|
||||||
ztd::option('c', "stdout", false, "Output result script to stdout"),
|
ztd::option('c', "stdout", false, "Output result script to stdout"),
|
||||||
ztd::option('e', "exec", false, "Directly execute script"),
|
ztd::option('e', "exec", false, "Directly execute script"),
|
||||||
ztd::option("no-shebang", false, "Don't output shebang"),
|
ztd::option("no-shebang", false, "Don't output shebang"),
|
||||||
|
ztd::option('P', "map", true , "Output var and fct minify map to given file", "file"),
|
||||||
|
ztd::option('A', "apply-map", true , "Apply map from given file", "file"),
|
||||||
#ifdef DEBUG_MODE
|
#ifdef DEBUG_MODE
|
||||||
ztd::option("\r [Debugging]"),
|
ztd::option("\r [Debugging]"),
|
||||||
ztd::option('J', "json", false, "Output the json structure"),
|
ztd::option('J', "json", false, "Output the json structure"),
|
||||||
|
|
@ -73,6 +75,18 @@ void get_opts()
|
||||||
options["minify-fct"].activated=true;
|
options["minify-fct"].activated=true;
|
||||||
options["remove-unused"].activated=true;
|
options["remove-unused"].activated=true;
|
||||||
}
|
}
|
||||||
|
if(options['o'].argument == "-")
|
||||||
|
options['o'].argument = "/dev/stdout";
|
||||||
|
if(options['P'].argument == "-")
|
||||||
|
options['P'].argument = "/dev/stdout";
|
||||||
|
if(options['A'].argument == "-")
|
||||||
|
options['A'].argument = "/dev/stdin";
|
||||||
|
if(
|
||||||
|
options['A'] && ( options['P'] || options["minify-var"] || options["minify-fct"] )
|
||||||
|
) {
|
||||||
|
printf("Incompatible options\n");
|
||||||
|
exit(ERR_OPT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ztd::option_set create_include_opts()
|
ztd::option_set create_include_opts()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue