Search in sources :

Example 26 with Configuration

use of org.walkmod.conf.entities.Configuration in project walkmod-core by walkmod.

the class YAMLConfigurationProviderTest method testAddChainTransformation.

@Test
public void testAddChainTransformation() throws Exception {
    Map<String, String> params = new HashMap<String, String>();
    params.put("refactoringConfigFile", "src/conf/refactoring-methods.json");
    AddTransformationCommand command = new AddTransformationCommand("walkmod:commons:method-refactor", null, false, null, null, null, params, false);
    File file = new File("src/test/resources/yaml/addchain.yml");
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
    FileUtils.write(file, "");
    try {
        YAMLConfigurationProvider provider = new YAMLConfigurationProvider(file.getPath());
        Configuration conf = new ConfigurationImpl();
        provider.init(conf);
        TransformationConfig transformationCfg = command.buildTransformationCfg();
        provider.addTransformationConfig(null, null, transformationCfg, false, null, null);
        String output = FileUtils.readFileToString(file);
        String desiredOutput = "transformations:\n";
        desiredOutput += "- type: \"walkmod:commons:method-refactor\"\n";
        desiredOutput += "  params:\n";
        desiredOutput += "    refactoringConfigFile: \"src/conf/refactoring-methods.json\"";
        Assert.assertEquals(desiredOutput, output);
    } finally {
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) HashMap(java.util.HashMap) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) AddTransformationCommand(org.walkmod.commands.AddTransformationCommand) File(java.io.File) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) Test(org.junit.Test)

Example 27 with Configuration

use of org.walkmod.conf.entities.Configuration in project walkmod-core by walkmod.

the class YAMLConfigurationProviderTest method testAddPluginRecursively.

@Test
public void testAddPluginRecursively() throws Exception {
    File dir = new File("src/test/resources/multimoduleyaml");
    dir.mkdirs();
    File module0 = new File(dir, "module0");
    module0.mkdir();
    File moduleCfg = new File(module0, "walkmod.yml");
    File file = new File(dir, "walkmod.yml");
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
    try {
        YAMLConfigurationProvider provider = new YAMLConfigurationProvider(file.getPath());
        Configuration conf = new ConfigurationImpl();
        provider.init(conf);
        provider.addModules(Arrays.asList("module0"));
        PluginConfig pluginCfg = new PluginConfigImpl();
        pluginCfg.setGroupId("org.walkmod");
        pluginCfg.setArtifactId("myplugin");
        pluginCfg.setVersion("1.0");
        provider.addPluginConfig(pluginCfg, true);
        String output = FileUtils.readFileToString(moduleCfg);
        String desiredOutput = "plugins:\n";
        desiredOutput += "- \"org.walkmod:myplugin:1.0\"";
        Assert.assertEquals(desiredOutput, output);
    } finally {
        if (dir.exists()) {
            FileUtils.deleteDirectory(dir);
        }
    }
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) Configuration(org.walkmod.conf.entities.Configuration) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) File(java.io.File) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) Test(org.junit.Test)

Example 28 with Configuration

use of org.walkmod.conf.entities.Configuration in project walkmod-core by walkmod.

the class DefaultChainWalkerAdapter method prepare.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void prepare() throws WalkModException {
    walker.setResource(getModel());
    walker.setRootNamespace(config.getRootNamespace());
    ChainWriter mw = ap.getChainWriter();
    mw.setExcludes(config.getChainConfig().getWriterConfig().getExcludes());
    mw.setIncludes(config.getChainConfig().getWriterConfig().getIncludes());
    walker.setWriter(ap.getChainWriter());
    walker.setChainConfig(config.getChainConfig());
    ChainConfig ac = config.getChainConfig();
    Object visitor;
    Configuration c = ac.getConfiguration();
    Parser parser = null;
    String parserType = config.getParserConfig().getType();
    if (parserType != null) {
        Object parserInstance = c.getBean(parserType, config.getParserConfig().getParameters());
        if (parserInstance != null) {
            if (parserInstance instanceof Parser) {
                parser = (Parser) parserInstance;
                walker.setParser(parser);
            } else {
                throw new WalkModException("The parser " + parserType + " must implement " + Parser.class.getName());
            }
        } else {
            throw new WalkModException("The parser " + parserType + " does not exist.");
        }
    }
    Collection<TransformationConfig> cfgs = getTransformationConfig();
    if (cfgs != null) {
        for (TransformationConfig config : cfgs) {
            setName(config.getName());
            visitor = config.getVisitorInstance();
            if (visitor == null || "".equals(config.getType())) {
                visitor = c.getBean(config.getType(), config.getParameters());
            }
            if (visitor instanceof ResourceModifier) {
                ((ResourceModifier) visitor).setResource(getModel());
            }
            if (visitor instanceof ParserAware) {
                ((ParserAware) visitor).setParser(parser);
            }
            if (visitor != null) {
                LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].walker = " + walker.getClass().getName());
                LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].visitor = " + visitor.getClass().getName());
                visitors.add(visitor);
            } else {
                walker = null;
                visitor = null;
                LOG.debug("Transformation[" + getName() + "] without walker and visitor");
            }
        }
    }
    walker.setVisitors(visitors);
    wi.init(this);
}
Also used : ChainWriter(org.walkmod.ChainWriter) ResourceModifier(org.walkmod.ResourceModifier) WalkModException(org.walkmod.exceptions.WalkModException) Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ParserAware(org.walkmod.walkers.ParserAware) ChainConfig(org.walkmod.conf.entities.ChainConfig) Parser(org.walkmod.walkers.Parser)

Example 29 with Configuration

use of org.walkmod.conf.entities.Configuration in project walkmod-core by walkmod.

the class WalkModFacade method install.

/**
 * Downloads the list of declared plugins in the configuration file using Ivy. Ignores the
 * ConfigurationProvided if passed in the constructor.
 *
 * @throws InvalidConfigurationException
 *             if the walkmod configuration is invalid and it is working in no verbose mode.
 */
public void install() throws InvalidConfigurationException {
    if (cfg.exists()) {
        if (options.isVerbose()) {
            log.info(cfg.getAbsoluteFile() + " [ok]");
        }
        // Uses Ivy always
        ConfigurationProvider cp = new IvyConfigurationProvider(options.isOffline(), options.isVerbose());
        if (options.isVerbose()) {
            log.info("** THE PLUGIN INSTALLATION STARTS **");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
        long startTime = System.currentTimeMillis();
        long endTime = startTime;
        DecimalFormat myFormatter = new DecimalFormat("###.###");
        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.US);
        boolean error = false;
        try {
            userDir = new File(System.getProperty("user.dir")).getAbsolutePath();
            System.setProperty("user.dir", options.getExecutionDirectory().getCanonicalPath());
            ConfigurationManager cfgManager = new ConfigurationManager(cfg, cp);
            Configuration cf = cfgManager.getConfiguration();
            List<String> modules = cf.getModules();
            if (modules != null && !modules.isEmpty()) {
                for (String module : modules) {
                    File aux = new File(module).getAbsoluteFile();
                    if (aux.isDirectory()) {
                        if (options.isVerbose()) {
                            log.info("** MODULE " + aux.getAbsoluteFile() + " [ok] **");
                        }
                        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options(options).executionDirectory(aux).build());
                        facade.install();
                    } else {
                        log.error("The module " + aux.getAbsolutePath() + " is not an existing directory");
                    }
                }
            }
        } catch (Exception e) {
            System.setProperty("user.dir", userDir);
            if (options.isVerbose()) {
                error = true;
                endTime = System.currentTimeMillis();
                double time = 0;
                if (endTime > startTime) {
                    time = (double) (endTime - startTime) / (double) 1000;
                }
                String timeMsg = myFormatter.format(time);
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("PLUGIN INSTALLATION FAILS");
                System.out.println();
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Total time: " + timeMsg + " seconds");
                log.info("Finished at: " + df.format(new Date()));
                log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                if (options.isPrintErrors()) {
                    log.error("Plugin installations fails", e);
                } else {
                    log.info("Plugin installations fails. Please, execute walkmod with -e to see the details");
                }
                if (options.isThrowException()) {
                    RuntimeException re = new RuntimeException();
                    re.setStackTrace(e.getStackTrace());
                    throw re;
                }
            } else {
                throw new InvalidConfigurationException(e);
            }
        }
        if (!error) {
            System.setProperty("user.dir", userDir);
            if (options.isVerbose()) {
                endTime = System.currentTimeMillis();
                double time = 0;
                if (endTime > startTime) {
                    time = (double) (endTime - startTime) / (double) 1000;
                }
                String timeMsg = myFormatter.format(time);
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                System.out.println();
                log.info("PLUGIN INSTALLATION COMPLETE");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Total time: " + timeMsg + " seconds");
                log.info("Finished at: " + df.format(new Date()));
                log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
            }
        }
    } else {
        if (options.isVerbose()) {
            log.error(cfg.getAbsolutePath() + " does not exist. The root directory of your project must contain a walkmod.xml");
        } else {
            throw new WalkModException(cfg.getAbsolutePath() + " does not exist. The root directory of your project must contain a walkmod.xml");
        }
    }
}
Also used : WalkModException(org.walkmod.exceptions.WalkModException) Configuration(org.walkmod.conf.entities.Configuration) DynamicModulesConfigurationProvider(org.walkmod.conf.providers.DynamicModulesConfigurationProvider) ConfigurationProvider(org.walkmod.conf.ConfigurationProvider) ProjectConfigurationProvider(org.walkmod.conf.ProjectConfigurationProvider) DynamicConfigurationProvider(org.walkmod.conf.providers.DynamicConfigurationProvider) IvyConfigurationProvider(org.walkmod.conf.providers.IvyConfigurationProvider) DecimalFormat(java.text.DecimalFormat) WalkModException(org.walkmod.exceptions.WalkModException) IOException(java.io.IOException) InvalidConfigurationException(org.walkmod.exceptions.InvalidConfigurationException) Date(java.util.Date) InvalidConfigurationException(org.walkmod.exceptions.InvalidConfigurationException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) ConfigurationManager(org.walkmod.conf.ConfigurationManager) IvyConfigurationProvider(org.walkmod.conf.providers.IvyConfigurationProvider)

Example 30 with Configuration

use of org.walkmod.conf.entities.Configuration in project walkmod-core by walkmod.

the class WalkModFacade method getConfiguration.

/**
 * Returns the equivalent configuration representation of the Walkmod config file.
 *
 * @return Configuration object representation of the config file.
 * @throws Exception
 *             If the walkmod configuration file can't be read.
 */
public Configuration getConfiguration() throws Exception {
    Configuration result = null;
    if (cfg.exists()) {
        userDir = new File(System.getProperty("user.dir")).getAbsolutePath();
        System.setProperty("user.dir", options.getExecutionDirectory().getAbsolutePath());
        try {
            ConfigurationManager manager = new ConfigurationManager(cfg, false);
            manager.executeConfigurationProviders();
            result = manager.getConfiguration();
        } finally {
            System.setProperty("user.dir", userDir);
        }
    }
    return result;
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) File(java.io.File) ConfigurationManager(org.walkmod.conf.ConfigurationManager)

Aggregations

Configuration (org.walkmod.conf.entities.Configuration)58 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)48 Test (org.junit.Test)43 File (java.io.File)38 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)23 LinkedList (java.util.LinkedList)21 AddTransformationCommand (org.walkmod.commands.AddTransformationCommand)21 ChainConfig (org.walkmod.conf.entities.ChainConfig)11 PluginConfig (org.walkmod.conf.entities.PluginConfig)10 HashMap (java.util.HashMap)6 WalkModException (org.walkmod.exceptions.WalkModException)6 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)5 IOException (java.io.IOException)5 WalkModFacade (org.walkmod.WalkModFacade)5 ConfigurationManager (org.walkmod.conf.ConfigurationManager)5 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)5 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)5 Document (org.w3c.dom.Document)4 NodeList (org.w3c.dom.NodeList)4 InvalidConfigurationException (org.walkmod.exceptions.InvalidConfigurationException)4