<commit_before>/**
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * Licensed 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 net.oneandone.stool.setup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import net.oneandone.inline.Console;
import net.oneandone.stool.cli.Import;
import net.oneandone.stool.cli.Main;
import net.oneandone.stool.configuration.Bedroom;
import net.oneandone.stool.configuration.StageConfiguration;
import net.oneandone.stool.configuration.StoolConfiguration;
import net.oneandone.stool.util.Logging;
import net.oneandone.stool.util.Session;
import net.oneandone.sushi.fs.file.FileNode;
import net.oneandone.sushi.util.Diff;
import net.oneandone.sushi.util.Separator;
import net.oneandone.sushi.util.Strings;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpgradeBuilder {
    private final Console console;
    private final Home home;
    private final FileNode from;

    // assigned during upgrade:
    private StoolConfiguration stoolRaw;
    private Session session = null;
    private String currentStage = null;

    /** maps names to ids */
    private Map<String, String> stages = new HashMap<>();

    public UpgradeBuilder(Console console, Home home, FileNode from) {
        this.console = console;
        this.home = home;
        this.from = from;
    }

    public String version() throws IOException {
        return from.join("version").readString().trim();
    }

    public void run() throws IOException {
        String oldVersion;
        String newVersion;
        Upgrade stage33_34;

        newVersion = Main.versionString(from.getWorld());
        oldVersion = version();
        if (!oldVersion.startsWith("3.3.")) {
            throw new IOException("don't know how to upgrade " + oldVersion + " -> " + newVersion);
        }
        console.info.println("upgrade " + oldVersion + " -> " + newVersion);
        stage33_34 = stage33_34();
        all(stool33_34(stage33_34), stage33_34);
    }


    private void all(Upgrade stoolMapper, Upgrade stageMapper) throws IOException {
        String id;
        FileNode stage;
        Logging logging;
        Import i;

        stool(from, stoolMapper);
        logging = Logging.forHome(home.dir);
        session = Session.load(false, home.dir, logging, "upgrade", console, home.dir.getWorld(), null, null);
        for (FileNode oldBackstage : from.join("backstages").list()) {
            console.info.println("upgrade " + oldBackstage);
            currentStage = oldBackstage.getName();
            id = getId(oldBackstage.join("config.json"));
            stages.put(currentStage, id);
            stage = oldBackstage.join("anchor").resolveLink();<commit_msg>skip existing stages<commit_after>/**
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * Licensed 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 net.oneandone.stool.setup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import net.oneandone.inline.Console;
import net.oneandone.stool.cli.Import;
import net.oneandone.stool.cli.Main;
import net.oneandone.stool.configuration.Bedroom;
import net.oneandone.stool.configuration.StageConfiguration;
import net.oneandone.stool.configuration.StoolConfiguration;
import net.oneandone.stool.util.Logging;
import net.oneandone.stool.util.Session;
import net.oneandone.sushi.fs.file.FileNode;
import net.oneandone.sushi.util.Diff;
import net.oneandone.sushi.util.Separator;
import net.oneandone.sushi.util.Strings;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpgradeBuilder {
    private final Console console;
    private final Home home;
    private final FileNode from;

    // assigned during upgrade:
    private StoolConfiguration stoolRaw;
    private Session session = null;
    private String currentStage = null;

    /** maps names to ids */
    private Map<String, String> stages = new HashMap<>();

    public UpgradeBuilder(Console console, Home home, FileNode from) {
        this.console = console;
        this.home = home;
        this.from = from;
    }

    public String version() throws IOException {
        return from.join("version").readString().trim();
    }

    public void run() throws IOException {
        String oldVersion;
        String newVersion;
        Upgrade stage33_34;

        newVersion = Main.versionString(from.getWorld());
        oldVersion = version();
        if (!oldVersion.startsWith("3.3.")) {
            throw new IOException("don't know how to upgrade " + oldVersion + " -> " + newVersion);
        }
        console.info.println("upgrade " + oldVersion + " -> " + newVersion);
        stage33_34 = stage33_34();
        all(stool33_34(stage33_34), stage33_34);
    }


    private void all(Upgrade stoolMapper, Upgrade stageMapper) throws IOException {
        String id;
        FileNode stage;
        Logging logging;
        Import i;

        stool(from, stoolMapper);
        logging = Logging.forHome(home.dir);
        session = Session.load(false, home.dir, logging, "upgrade", console, home.dir.getWorld(), null, null);
        for (FileNode oldBackstage : from.join("backstages").list()) {
            id = getId(oldBackstage.join("config.json"));
            currentStage = oldBackstage.getName();
            if (home.dir.join("backstages", id).isDirectory()) {
                console.info.println("stage already imported, ignored: " + currentStage + " (" + id + ")");
                continue;
            }
            console.info.println("import " + oldBackstage);
            stages.put(currentStage, id);
            stage = oldBackstage.join("anchor").resolveLink();
{'fraction_non_alphanumeric': 0.10851629142777235, 'fraction_numerical': 0.009509159558103763, 'mean_word_length': 3.226950354609929, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '15235', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2015, 'n_words': 549}