|
| 1 | +/* |
| 2 | + * This file is part of AndroidIDE. |
| 3 | + * |
| 4 | + * AndroidIDE is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * AndroidIDE is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package com.itsaky.androidide.utils; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +/** |
| 24 | + * One-time rename of the fork-era {@code .androidide} directory to {@link |
| 25 | + * SharedEnvironment#PROJECT_CACHE_DIR_NAME}. |
| 26 | + */ |
| 27 | +public final class LegacyIdeDataDirMigration { |
| 28 | + |
| 29 | + private static final Logger LOG = LoggerFactory.getLogger(LegacyIdeDataDirMigration.class); |
| 30 | + |
| 31 | + private LegacyIdeDataDirMigration() {} |
| 32 | + |
| 33 | + /** |
| 34 | + * Resolves which IDE data directory path is in use: prefers {@code current} after a successful |
| 35 | + * rename from {@code legacy}. If {@code current} already exists, it wins (both-existing case is |
| 36 | + * logged). If only {@code legacy} exists and {@code legacy.renameTo(current)} fails, returns |
| 37 | + * {@code legacy} so callers keep using the existing tree. If neither exists, returns {@code |
| 38 | + * current} for the caller to create. |
| 39 | + */ |
| 40 | + public static File migrateLegacyIdeDataDirIfNeeded(File legacy, File current) { |
| 41 | + if (current.exists() && legacy.exists()) { |
| 42 | + LOG.warn( |
| 43 | + "Both {} and {} exist; using {} only", |
| 44 | + legacy.getAbsolutePath(), |
| 45 | + current.getAbsolutePath(), |
| 46 | + current.getAbsolutePath()); |
| 47 | + return current; |
| 48 | + } |
| 49 | + if (legacy.exists()) { |
| 50 | + if (legacy.renameTo(current)) { |
| 51 | + return current; |
| 52 | + } |
| 53 | + LOG.warn("Failed to rename legacy IDE data dir {} to {}", legacy, current); |
| 54 | + return legacy; |
| 55 | + } |
| 56 | + return current; |
| 57 | + } |
| 58 | +} |
0 commit comments