<commit_before>/**
 * Copyright 2005-2015 Red Hat, Inc.
 * <p>
 * Red Hat licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package io.fabric8.forge.devops;

import io.fabric8.forge.addon.utils.ProfilesProjectHelper;
import io.fabric8.forge.addon.utils.StopWatch;
import io.fabric8.forge.devops.setup.Fabric8SetupStep;
import io.fabric8.forge.devops.setup.SetupProjectHelper;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UINavigationContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.addon.ui.wizard.UIWizard;

import static io.fabric8.forge.devops.setup.Fabric8SetupStep.setupSitePlugin;

public class DevOpsEditCommand extends AbstractDevOpsCommand implements UIWizard {

    private volatile boolean needFabric8Setup;

    @Override
    public UICommandMetadata getMetadata(UIContext context) {
        return Metadata.forCommand(getClass())
                .category(Categories.create(AbstractDevOpsCommand.CATEGORY))
                .name(AbstractDevOpsCommand.CATEGORY + ": Edit")
                .description("Edit the DevOps configuration for this project");
    }

    @Override
    public void initializeUI(UIBuilder builder) throws Exception {
        StopWatch watch = new StopWatch();
        try {
            Project project = getSelectedProject(builder.getUIContext());
            log.info("initializeUI#getSelectedProject taken " + watch.taken());
            if (project != null) {
                setupSitePlugin(project);
                log.info("initializeUI#setupSitePlugin taken " + watch.taken());

                if (ProfilesProjectHelper.isProfilesProject(project)) {
                    // TODO: in the future we might want to verify the setup of a profiles project here.
                } else if (!SetupProjectHelper.fabric8ProjectSetupCorrectly(project)) {
                    needFabric8Setup = true;
                }
                log.info("initializeUI#fabric8ProjectSetupCorrectly taken " + watch.taken());
            }
        } catch (IllegalStateException e) {
            // ignore lack of project
        }
        log.info("initializeUI took " + watch.taken());
    }

    @Override
    public NavigationResult next(UINavigationContext context) throws Exception {
        NavigationResultBuilder builder = NavigationResultBuilder.create();
        if (needFabric8Setup) {
            builder.add(Fabric8SetupStep.class);
        }
        builder.add(DevOpsEditStep.class);
        builder.add(SaveDevOpsStep.class);
        return builder.build();
    }

    @Override
    public Result execute(UIExecutionContext context) throws Exception {
        return Results.success();
    }
}<commit_msg>#704: Optimize devops-edit command to be faster<commit_after>/**
 * Copyright 2005-2015 Red Hat, Inc.
 * <p>
 * Red Hat licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package io.fabric8.forge.devops;

import java.io.File;
import java.io.FileInputStream;

import io.fabric8.forge.addon.utils.StopWatch;
import io.fabric8.forge.devops.setup.Fabric8SetupStep;
import io.fabric8.forge.devops.springboot.IOHelper;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UINavigationContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.addon.ui.wizard.UIWizard;

public class DevOpsEditCommand extends AbstractDevOpsCommand implements UIWizard {

    private volatile boolean needFabric8Setup = true;

    @Override
    public UICommandMetadata getMetadata(UIContext context) {
        return Metadata.forCommand(getClass())
                .category(Categories.create(AbstractDevOpsCommand.CATEGORY))
                .name(AbstractDevOpsCommand.CATEGORY + ": Edit")
                .description("Edit the DevOps configuration for this project");
    }

    @Override
    public void initializeUI(UIBuilder builder) throws Exception {
        StopWatch watch = new StopWatch();

        // load maven pom.xml
        File rootFolder = getSelectionFolder(builder.getUIContext());
        if (rootFolder != null) {
            File pom = new File(rootFolder, "pom.xml");
            if (pom.exists() && pom.isFile()) {
                String text = IOHelper.loadText(new FileInputStream(pom));
                if (text.contains("fabric8-profiles")) {
                    // no need for fabric8 for fuse projects
                    needFabric8Setup = false;
                } else if (text.contains("io.fabric8.funktion")) {
                    // no need for fabric8 for funktion projects
                    needFabric8Setup = false;
                } else if (text.contains("fabric8-maven-plugin")) {
                    // no need for fabric8 if we have f-m-p plugin
                    needFabric8Setup = false;
                }
            }
        }

        log.info("Need fabric8 setup? " + needFabric8Setup);

        log.info("initializeUI took " + watch.taken());
    }

    // the following old code is slow so we optimize in a different way
    // https://github.com/fabric8io/fabric8-forge/issues/704
    /*
    @Override
    public void initializeUI(UIBuilder builder) throws Exception {
        StopWatch watch = new StopWatch();
        try {
            Project project = getSelectedProject(builder.getUIContext());
            log.info("initializeUI#getSelectedProject taken " + watch.taken());
            if (project != null) {
                setupSitePlugin(project);
                log.info("initializeUI#setupSitePlugin taken " + watch.taken());

                if (ProfilesProjectHelper.isProfilesProject(project)) {
                    // TODO: in the future we might want to verify the setup of a profiles project here.
                } else if (!SetupProjectHelper.fabric8ProjectSetupCorrectly(project)) {
                    needFabric8Setup = true;
                }
                log.info("initializeUI#fabric8ProjectSetupCorrectly taken " + watch.taken());
            }
        } catch (IllegalStateException e) {
            // ignore lack of project
        }
        log.info("initializeUI took " + watch.taken());
    }*/

    @Override
    public NavigationResult next(UINavigationContext context) throws Exception {
        NavigationResultBuilder builder = NavigationResultBuilder.create();
        if (needFabric8Setup) {
            builder.add(Fabric8SetupStep.class);
        }
        builder.add(DevOpsEditStep.class);
        builder.add(SaveDevOpsStep.class);
        return builder.build();
    }

    @Override
    public Result execute(UIExecutionContext context) throws Exception {
        return Results.success();
    }
}
{'fraction_non_alphanumeric': 0.09588724584103513, 'fraction_numerical': 0.007855822550831794, 'mean_word_length': 2.8305309734513275, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5031349', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2333, 'n_words': 588}