Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. param (
  2. [string]$app_root = "c:\hadbadge2019_fpgasoc",
  3. [string]$toolchain_root = "C:\2019_SuperCon\ecp5-toolchain-windows-v1.6.9\bin"
  4. )
  5.  
  6. function read_config($app_root,$config_filename = "config.ini")
  7. {
  8.  
  9. Push-Location $app_root
  10.  
  11. #$app_list = Get-ChildItem $app_root\app*\*main.c -Recurse
  12. $config_file = Get-ChildItem .\$config_filename
  13.  
  14. if ($config_file.Count -lt 1) {
  15. Write-Host 'Unable to find config.ini!!!'
  16. return 0
  17. } else {
  18. $settings = Get-IniContent .\$config_filename
  19. }
  20.  
  21. return $settings
  22. }
  23.  
  24.  
  25. function run_daemon()
  26. {
  27. param (
  28. [string]$app_root,
  29. [string]$toolchain_root
  30. )
  31.  
  32. # Add toolchain root to path variable
  33. if($env:Path -notlike $toolchain_root){
  34. $env:Path += ";$toolchain_root"
  35. }
  36.  
  37. $config = read_config($app_root)
  38. $main_file_list = @{}
  39. $config.Keys | % {
  40. $root= $config.$_.app_root_folder
  41. $drive = $config.$_.device_drive
  42.  
  43. Push-Location $root
  44.  
  45. if (!(Test-Path $root\main.c)) {
  46. Write-Host "Unable to find main.c for $root...Skipping..."
  47. }else{
  48. $main_file_list += @{
  49. "$($_)" = @{
  50. "app_root_folder" = $root;
  51. "file_info" = $(Get-ChildItem .\main.c);
  52. "prev_file_info" = $(Get-ChildItem .\*)[-1];
  53. "device_drive" = $drive
  54. "name" = $_
  55. }
  56. }
  57. }
  58. }
  59.  
  60. while($true) {
  61. $main_file_list.Keys | % {
  62. $item = $main_file_list.$_
  63.  
  64. Push-Location $item.app_root_folder
  65.  
  66. $main_file_list.$_.file_info = $(Get-ChildItem .\main.c)
  67. $drive = $item.device_drive
  68. # Write-Host $item.Keys
  69. # read-host "Drive: $drive"
  70.  
  71. if($main_file_list.$_.file_info.LastWriteTime -gt $main_file_list.$_.prev_file_info.LastWriteTime) {
  72. # Check the drive
  73. if(!(Get-PSDrive -Name $drive -ErrorAction SilentlyContinue)){
  74. Write-Host "Attempting to mount the disk..."
  75. Get-Disk -FriendlyName HADBADGE* | Get-Partition | Set-Partition -NewDriveLetter $drive -ErrorAction SilentlyContinue
  76. Start-Sleep 2
  77. if(!(Get-PSDrive -Name $drive -ErrorAction SilentlyContinue)){
  78. Write-Host "Errors mounting the $drive drive!!!"
  79. Write-Host "Have you tried turning it off and on again? ^_^" -BackgroundColor White -ForegroundColor Red
  80. Continue
  81. }
  82. }
  83.  
  84. # Make the file
  85. Write-Host "Change Places! \nMake the ELF!..."
  86. Invoke-Expression "powershell.exe make"
  87.  
  88. # Push the file
  89. Write-Host "Uploading code to badge..."
  90. $filename = $item.name
  91. # Read-Host $filename
  92. $command = ""
  93. $command = "Copy-Item '$($item.app_root_folder)\$filename.elf' '$drive`:\$filename.elf'"
  94. Write-Host "$command"
  95. Invoke-Expression "$command"
  96. if(!$error){
  97. Write-Host "Successfully wrote new ELF to badge ^_^"
  98. }
  99.  
  100. # Lick the .... Dismount the filesystem ^_^
  101. $vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq "$drive"+':\'}
  102. $vol.DriveLetter = $null
  103. $vol.Put()
  104. $vol.Dismount($false, $false)
  105.  
  106. # Set previous main.c file
  107. $main_file_list.$_.prev_file_info = $main_file_list.$_.file_info
  108. } else {
  109. Write-Host "Waiting for changes to $_ main.c file..."
  110. }
  111. }
  112. Start-Sleep 5
  113. }
  114. }
  115.  
  116. function Get-IniContent ($filePath)
  117. {
  118. $ini = @{}
  119. switch -regex -file $FilePath
  120. {
  121. "^\[(.+)\]" # Section
  122. {
  123. $section = $matches[1]
  124. $ini[$section] = @{}
  125. $CommentCount = 0
  126. }
  127. "^(;.*)$" # Comment
  128. {
  129. $value = $matches[1]
  130. $CommentCount = $CommentCount + 1
  131. $name = “Comment” + $CommentCount
  132. $ini[$section][$name] = $value
  133. }
  134. "(.+?)\s*=(.*)" # Key
  135. {
  136. $name,$value = $matches[1..2]
  137. $ini[$section][$name] = $value
  138. }
  139. }
  140. return $ini
  141. }
  142.  
  143. # Run the daemon
  144. Write-Host "Daemon starting up..."
  145. run_daemon -app_root $app_root -toolchain_root $toolchain_root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement