<commit_before>package eu.netide.configuration.launcher.managers

import eu.netide.configuration.preferences.NetIDEPreferenceConstants
import eu.netide.configuration.utils.NetIDE
import java.io.File
import java.util.ArrayList
import java.util.HashMap
import java.util.Map
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.CoreException
import org.eclipse.core.runtime.IProgressMonitor
import org.eclipse.core.runtime.IStatus
import org.eclipse.core.runtime.Path
import org.eclipse.core.runtime.Platform
import org.eclipse.core.runtime.Status
import org.eclipse.debug.core.DebugPlugin
import org.eclipse.debug.core.ILaunch
import org.eclipse.debug.core.RefreshUtil
import org.eclipse.debug.core.model.IProcess
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.core.runtime.jobs.Job
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.regex.Pattern
import org.eclipse.debug.core.Launch
import org.eclipse.debug.core.ILaunchConfiguration
import java.util.Date
import org.eclipse.ui.internal.console.ConsoleManager
import org.eclipse.debug.internal.core.LaunchManager
import org.eclipse.core.resources.IProject
import org.eclipse.xtend.lib.annotations.Accessors

class VagrantManager implements IManager {

	private ILaunch launch

	private String vagrantpath

	private File workingDirectory

	private IProgressMonitor monitor
	
	@Accessors(PUBLIC_GETTER)
	private IProject project

	new(ILaunchConfiguration launchConfiguration, IProgressMonitor monitor) {

		this.launch = new Launch(launchConfiguration, "debug", null)
		this.launch.setAttribute("org.eclipse.debug.core.capture_output", "true")
		this.launch.setAttribute("org.eclipse.debug.ui.ATTR_CONSOLE_ENCODING", "UTF-8")
		this.launch.setAttribute("org.eclipse.debug.core.launch.timestamp", new Date().time + "")
		DebugPlugin.getDefault().getLaunchManager().addLaunch(this.launch)

		this.monitor = monitor

		this.vagrantpath = new Path(
			Platform.getPreferencesService.getString(NetIDEPreferenceConstants.ID,
				NetIDEPreferenceConstants.VAGRANT_PATH, "", null)).toOSString

		var path = launch.launchConfiguration.attributes.get("topologymodel") as String

		this.workingDirectory = path.getIFile.project.location.append("/gen" + NetIDE.VAGRANTFILE_PATH).toFile
		
		var topofile = launchConfiguration.getAttribute("topologymodel", "").IFile
		this.project = topofile.project
	}

	def up() {
		var cmd = newArrayList(vagrantpath, "up")
		startProcess(cmd)
	}

	override asyncUp() {
		var job = new Job("Vagrant Up") {
			override run(IProgressMonitor monitor) {
				up()
				Status.OK_STATUS
			}
		}
		job.schedule()
	}

	def halt() {
		var cmd = newArrayList(vagrantpath, "halt")
		startProcess(cmd)
	}

	override asyncHalt() {
		var command = new Job("Vagrant Halt") {
			override run(IProgressMonitor monitor) {
				halt()
				Status.OK_STATUS
			}
		}
		command.schedule
	}

	override provision() {
		var cmd = newArrayList(vagrantpath, "provision")
		startProcess(cmd)
	}

	override asyncProvision() {
		var command = new Job("Vagrant Provision") {
			override run(IProgressMonitor monitor) {
				provision()
				Status.OK_STATUS
			}
		}
		command.schedule
	}

	def init() {

		var cmd = newArrayList(vagrantpath, "init", "ubuntu/trusty32")
		startProcess(cmd)
	}

	def asyncInit() {
		var command = new Job("Vagrant Init") {
			override run(IProgressMonitor monitor) {
				init()
				Status.OK_STATUS
			}
		}
		command.schedule
	}

	override getRunningSessions() {
		var p = DebugPlugin.exec(newArrayList(vagrantpath, "ssh", "-c", "screen -list"), workingDirectory, null)
		var br = new BufferedReader(new InputStreamReader(p.getInputStream()))
		p.waitFor
		var output = newArrayList()
		var pattern = Pattern.compile("\\d+\\.[\\w\\.]+\\.\\d+")

		var l = br.readLine
		while (l != null) {
			var matcher = pattern.matcher(l)
			if (matcher.find())
				output.add(matcher.group)
			l = br.readLine

		}
		p.waitFor
		return output
	}
	
	override execWithReturn(String cmd) {
		var p = DebugPlugin.exec(newArrayList(vagrantpath, "ssh", "-c", cmd), workingDirectory, null)
		var br = new BufferedReader(new InputStreamReader(p.getInputStream()))
		p.waitFor
		var output = ""

		var l = br.readLine
		while (l != null) {
			output = l + "\n"
		}
		p.waitFor
		return output
	}


	def startProcess(ArrayList<String> cmdline) {

		var workingDir = this.workingDirectory
		var location = new Path(vagrantpath)
		var env = null

		var p = DebugPlugin.exec(cmdline, workingDir, env)

		var IProcess process = null;

		// add process type to process attributes<commit_msg>Topology import for vagrant manager<commit_after>package eu.netide.configuration.launcher.managers

import eu.netide.configuration.preferences.NetIDEPreferenceConstants
import eu.netide.configuration.utils.NetIDE
import java.io.File
import java.util.ArrayList
import java.util.HashMap
import java.util.Map
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.CoreException
import org.eclipse.core.runtime.IProgressMonitor
import org.eclipse.core.runtime.IStatus
import org.eclipse.core.runtime.Path
import org.eclipse.core.runtime.Platform
import org.eclipse.core.runtime.Status
import org.eclipse.debug.core.DebugPlugin
import org.eclipse.debug.core.ILaunch
import org.eclipse.debug.core.RefreshUtil
import org.eclipse.debug.core.model.IProcess
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.core.runtime.jobs.Job
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.regex.Pattern
import org.eclipse.debug.core.Launch
import org.eclipse.debug.core.ILaunchConfiguration
import java.util.Date
import org.eclipse.ui.internal.console.ConsoleManager
import org.eclipse.debug.internal.core.LaunchManager
import org.eclipse.core.resources.IProject
import org.eclipse.xtend.lib.annotations.Accessors

class VagrantManager implements IManager {

	private ILaunch launch

	private String vagrantpath

	private File workingDirectory

	private IProgressMonitor monitor
	
	@Accessors(PUBLIC_GETTER)
	private IProject project

	new(ILaunchConfiguration launchConfiguration, IProgressMonitor monitor) {

		this.launch = new Launch(launchConfiguration, "debug", null)
		this.launch.setAttribute("org.eclipse.debug.core.capture_output", "true")
		this.launch.setAttribute("org.eclipse.debug.ui.ATTR_CONSOLE_ENCODING", "UTF-8")
		this.launch.setAttribute("org.eclipse.debug.core.launch.timestamp", new Date().time + "")
		DebugPlugin.getDefault().getLaunchManager().addLaunch(this.launch)

		this.monitor = monitor

		this.vagrantpath = new Path(
			Platform.getPreferencesService.getString(NetIDEPreferenceConstants.ID,
				NetIDEPreferenceConstants.VAGRANT_PATH, "", null)).toOSString

		var path = launch.launchConfiguration.attributes.get("topologymodel") as String

		this.workingDirectory = path.getIFile.project.location.append("/gen" + NetIDE.VAGRANTFILE_PATH).toFile
		
		var topofile = launchConfiguration.getAttribute("topologymodel", "").IFile
		this.project = topofile.project
	}

	def up() {
		var cmd = newArrayList(vagrantpath, "up")
		startProcess(cmd)
	}

	override asyncUp() {
		var job = new Job("Vagrant Up") {
			override run(IProgressMonitor monitor) {
				up()
				Status.OK_STATUS
			}
		}
		job.schedule()
	}

	def halt() {
		var cmd = newArrayList(vagrantpath, "halt")
		startProcess(cmd)
	}

	override asyncHalt() {
		var command = new Job("Vagrant Halt") {
			override run(IProgressMonitor monitor) {
				halt()
				Status.OK_STATUS
			}
		}
		command.schedule
	}

	override provision() {
		var cmd = newArrayList(vagrantpath, "provision")
		startProcess(cmd)
	}

	override asyncProvision() {
		var command = new Job("Vagrant Provision") {
			override run(IProgressMonitor monitor) {
				provision()
				Status.OK_STATUS
			}
		}
		command.schedule
	}

	def init() {

		var cmd = newArrayList(vagrantpath, "init", "ubuntu/trusty32")
		startProcess(cmd)
	}

	def asyncInit() {
		var command = new Job("Vagrant Init") {
			override run(IProgressMonitor monitor) {
				init()
				Status.OK_STATUS
			}
		}
		command.schedule
	}

	override getRunningSessions() {
		var p = DebugPlugin.exec(newArrayList(vagrantpath, "ssh", "-c", "screen -list"), workingDirectory, null)
		var br = new BufferedReader(new InputStreamReader(p.getInputStream()))
		p.waitFor
		var output = newArrayList()
		var pattern = Pattern.compile("\\d+\\.[\\w\\.]+\\.\\d+")

		var l = br.readLine
		while (l != null) {
			var matcher = pattern.matcher(l)
			if (matcher.find())
				output.add(matcher.group)
			l = br.readLine

		}
		p.waitFor
		return output
	}
	
	override execWithReturn(String cmd) {
		var p = DebugPlugin.exec(newArrayList(vagrantpath, "ssh", "-c", cmd), workingDirectory, null)
		var br = new BufferedReader(new InputStreamReader(p.getInputStream()))
		p.waitFor
		var output = ""

		var l = br.readLine
		while (l != null) {
			output = l + "\n"
			l = br.readLine
		}
		return output
	}


	def startProcess(ArrayList<String> cmdline) {

		var workingDir = this.workingDirectory
		var location = new Path(vagrantpath)
		var env = null

		var p = DebugPlugin.exec(cmdline, workingDir, env)

		var IProcess process = null;

		// add process type to process attributes
{'fraction_non_alphanumeric': 0.1127739176910743, 'fraction_numerical': 0.0006413682522715125, 'mean_word_length': 4.43953488372093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3756978', 'n_tokens_mistral': 3250, 'n_tokens_neox': 2920, 'n_words': 606}