1/*******************************************************************************
2 * Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.cli.internal.commands;
13
14import java.io.File;
15import java.io.IOException;
16import java.io.PrintWriter;
17import java.util.ArrayList;
18import java.util.List;
19
20import org.jacoco.cli.internal.Command;
21import org.jacoco.core.tools.ExecFileLoader;
22import org.kohsuke.args4j.Argument;
23import org.kohsuke.args4j.Option;
24
25/**
26 * The <code>merge</code> command.
27 */
28public class Merge extends Command {
29
30	@Argument(usage = "list of JaCoCo *.exec files to read", metaVar = "<execfiles>")
31	List<File> execfiles = new ArrayList<File>();
32
33	@Option(name = "-destfile", usage = "file to write merged execution data to", metaVar = "<path>", required = true)
34	File destfile;
35
36	@Override
37	public String description() {
38		return "Merges multiple exec files into a new one.";
39	}
40
41	@Override
42	public int execute(final PrintWriter out, final PrintWriter err)
43			throws IOException {
44		final ExecFileLoader loader = loadExecutionData(out);
45		out.printf("[INFO] Writing execution data to %s.%n",
46				destfile.getAbsolutePath());
47		loader.save(destfile, true);
48		return 0;
49	}
50
51	private ExecFileLoader loadExecutionData(final PrintWriter out)
52			throws IOException {
53		final ExecFileLoader loader = new ExecFileLoader();
54		for (final File file : execfiles) {
55			out.printf("[INFO] Loading execution data file %s.%n",
56					file.getAbsolutePath());
57			loader.load(file);
58		}
59		return loader;
60	}
61
62}
63