view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialClasspathTemplate.java @ 1068:2d8aab94494f Lakhnau-2020.2.x release-86063

TW-70693
author victory.bedrosova
date Thu, 18 Mar 2021 18:30:34 +0100
parents 7bf4d943d5bb
children
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

import static com.intellij.openapi.util.io.FileUtil.createTempFile;
import static com.intellij.openapi.util.io.FileUtil.delete;

/**
* Created 25.02.14 11:29
*
* @author Eugene Petrenko (eugene.petrenko@jetbrains.com)
*/
public class MercurialClasspathTemplate implements MercurialTemplate {
  private final String myResourcePath;
  private final String myTmpFileSuffix;

  public MercurialClasspathTemplate(@NotNull final String resourcePath,
                                    @NotNull final String tmpFileSuffix) {
    myResourcePath = resourcePath;
    myTmpFileSuffix = tmpFileSuffix;
  }

  @NotNull
  public <T> T withTemplate(@NotNull final WithTemplate<T> action) throws VcsException {
    //TODO: we may pack plugin and use jetbrains.buildServer.web.openapi.PluginDescriptor.getPluginRoot()
    //TODO: to get path to existing file
    final File template = copyTemplate();
    try {
      return action.action(template);
    } finally {
      delete(template);
    }
  }

  private File copyTemplate() throws VcsException {
    try {
      final File template = createTempFile("teamcity", myTmpFileSuffix);
      FileUtil.copyResource(getClass(), myResourcePath, template);
      return template;
    } catch (IOException e) {
      throw new VcsException("Cannot create mercurial log template", e);
    }
  }
}