added ruby hook package_xrf.rb
This commit is contained in:
		
							parent
							
								
									a19c45cea7
								
							
						
					
					
						commit
						34f6e052da
					
				
					 1 changed files with 90 additions and 0 deletions
				
			
		
							
								
								
									
										90
									
								
								manyfold/root/hook.d/inotify_MODIFY/package_xrf.rb
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										90
									
								
								manyfold/root/hook.d/inotify_MODIFY/package_xrf.rb
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,90 @@
 | 
			
		|||
#!/usr/bin/env ruby
 | 
			
		||||
 | 
			
		||||
require 'json'
 | 
			
		||||
 | 
			
		||||
extensions = ['.glb', '.gltf', '.blend', '.usdz', '.obj', '.dae']
 | 
			
		||||
 | 
			
		||||
# Check if a filename is provided
 | 
			
		||||
if ARGV.length != 1
 | 
			
		||||
  puts "Usage: #{$0} <filename>"
 | 
			
		||||
  exit 1
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def log_message(message, filename, reset = false)
 | 
			
		||||
 | 
			
		||||
  # Empty the log file if reset is true
 | 
			
		||||
  if reset
 | 
			
		||||
    File.open(filename, 'w') do |file|
 | 
			
		||||
      timestamp = Time.now.strftime('%Y-%m-%d %H:%M')
 | 
			
		||||
      file.write("#{timestamp}\n\n")
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Create the full log entry
 | 
			
		||||
 | 
			
		||||
  # Append the log entry to the log file
 | 
			
		||||
  File.open(filename, 'a') do |file|
 | 
			
		||||
    file.write("#{message}\n")
 | 
			
		||||
  end
 | 
			
		||||
  puts("#{message}\n")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
filename = ARGV[0]
 | 
			
		||||
 | 
			
		||||
begin
 | 
			
		||||
  # Read and parse the JSON file
 | 
			
		||||
  data = JSON.parse(File.read(filename))
 | 
			
		||||
 | 
			
		||||
  logfile = File.join( File.dirname(filename), "xrfragment.txt" )
 | 
			
		||||
 | 
			
		||||
  log_message("✔ starting XR fragments check", logfile, true)
 | 
			
		||||
 | 
			
		||||
  # Extract the desired field (assuming the field is named 'model_file')
 | 
			
		||||
  thumb_file = data['image']
 | 
			
		||||
  log_message("✔ thumbnail sidecar-file '#{thumb_file}' detected", logfile)
 | 
			
		||||
  
 | 
			
		||||
  # Get the base name of the thumbnail file without its extension
 | 
			
		||||
  base_name = File.basename(thumb_file, File.extname(thumb_file))
 | 
			
		||||
  
 | 
			
		||||
  model_file = nil # Initialize model_file to nil
 | 
			
		||||
  
 | 
			
		||||
  # Loop over the list of extensions
 | 
			
		||||
  extensions.each do |ext|
 | 
			
		||||
    # Construct the filename with the current extension
 | 
			
		||||
    filename = "#{base_name}#{ext}"
 | 
			
		||||
  
 | 
			
		||||
    # Check if the file exists
 | 
			
		||||
    if File.exist?(filename)
 | 
			
		||||
      log_message("✔ 3D file '#{filename}' detected", logfile)
 | 
			
		||||
      model_file = filename # Store the found filename
 | 
			
		||||
      break # Stop the loop once a file is found
 | 
			
		||||
    else
 | 
			
		||||
      # Log a message for the file that was not found, but don't stop
 | 
			
		||||
      log_message("⚠️ 3D file '#{filename}' not detected", logfile)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
  
 | 
			
		||||
  # Check if a model file was found after the loop
 | 
			
		||||
  if model_file
 | 
			
		||||
    log_message("✅ Final model file: '#{model_file}'", logfile)
 | 
			
		||||
  else
 | 
			
		||||
    log_message("❌ No suitable 3D file found for XR Fragments-compatible experience", logfile)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Construct the output filename
 | 
			
		||||
  output_file = "#{File.basename(model_file, File.extname(model_file))}.blend"
 | 
			
		||||
 | 
			
		||||
  # Execute the system call
 | 
			
		||||
  puts("assimp", model_file, output_file)
 | 
			
		||||
  system("assimp", model_file, output_file)
 | 
			
		||||
 | 
			
		||||
rescue Errno::ENOENT
 | 
			
		||||
  puts "File #{filename} not found"
 | 
			
		||||
rescue JSON::ParserError
 | 
			
		||||
  puts "Error parsing JSON from #{filename}"
 | 
			
		||||
rescue => e
 | 
			
		||||
  puts "An error occurred: #{e.message}"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
log_message("", logfile)
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue