import os
from os.path import join
from .ConanTools import ConanTools
from .Utility import Utility

class PackageManagement(object):
	'''
	Provides functionality for managing Conan packages
	'''
	
	@staticmethod
	def install(packageDir, channel, profile, args=[]):
		'''
		Installs a Conan package
		'''
		return Utility.run(['conan', 'create', '.', 'adamrehn/' + channel, '--profile=' + profile] + args, cwd=packageDir)
	
	@staticmethod
	def generateWrapper(libName, template, delegates, packageDir, channel, profile):
		'''
		Generates and installs a wrapper package
		'''
		conanfile = template.replace('${LIBNAME}', libName)
		conanfile = conanfile.replace('${DELEGATE_CLASS}', delegates.getDelegateClass(libName))
		ConanTools.save(join(packageDir, 'conanfile.py'), conanfile)
		PackageManagement.install(packageDir, channel, profile)
	
	@staticmethod
	def removeBasePackages():
		'''
		Removes profile-wide base packages previously generated by conan-ue4cli
		'''
		Utility.run(['conan', 'remove', '--force', '*@adamrehn/profile'])
