Skip to content

Commit

Permalink
(GH-121) Load Puppet Functions via Puppet API v4 and present as Puppe…
Browse files Browse the repository at this point in the history
…t API v3 functions (#126)

(GH-121) Load Puppet Functions via Puppet API v4 and present as Puppet API v3 functions
  • Loading branch information
michaeltlombardi authored Jun 6, 2019
2 parents 08a2fda + 3e13a8a commit ca0175f
Show file tree
Hide file tree
Showing 28 changed files with 1,337 additions and 52 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ group :development do
else
gem 'puppet', :require => false
end
# TODO: This should be vendored into Editor Services after it is no longer a feature flag
# The puppet-strings gem is not available in the Puppet Agent, but is in the PDK. We add it to the
# Gemfile here for testing and development.
gem "puppet-strings", "~> 2.0", :require => false

case RUBY_PLATFORM
when /darwin/
Expand Down
7 changes: 7 additions & 0 deletions lib/puppet-languageserver-sidecar/cache/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Cache
CLASSES_SECTION = 'classes'
FUNCTIONS_SECTION = 'functions'
TYPES_SECTION = 'types'
PUPPETSTRINGS_SECTION = 'puppetstrings'

class Base
attr_reader :cache_options
Expand All @@ -24,6 +25,12 @@ def load(_absolute_path, _section)
def save(_absolute_path, _section, _content_string)
raise NotImplementedError
end

# WARNING - This method is only intended for testing the cache
# and should not be used for normal operations
def clear!
raise NotImplementedError
end
end
end
end
17 changes: 12 additions & 5 deletions lib/puppet-languageserver-sidecar/cache/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(_options = {})
begin
Dir.mkdir(@cache_dir) unless Dir.exist?(@cache_dir)
rescue Errno::ENOENT => e
PuppetLanguageServerSidecar.log_message(:error, "[PuppetLanguageServerSidecar::Cache::File] An error occured while creating file cache. Disabling cache: #{e}")
PuppetLanguageServerSidecar.log_message(:error, "[PuppetLanguageServerSidecar::Cache::FileSystem] An error occured while creating file cache. Disabling cache: #{e}")
@cache_dir = nil
end
end
Expand All @@ -37,20 +37,20 @@ def load(absolute_path, section)

# Check that this is from the same language server version
unless json_obj['sidecar_version'] == PuppetLanguageServerSidecar.version
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::load] Error loading #{absolute_path}: Expected sidecar_version version #{PuppetLanguageServerSidecar.version} but found #{json_obj['sidecar_version']}")
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Error loading #{absolute_path}: Expected sidecar_version version #{PuppetLanguageServerSidecar.version} but found #{json_obj['sidecar_version']}")
return nil
end
# Check that the source file hash matches
content_hash = calculate_hash(absolute_path)
if json_obj['file_hash'] != content_hash
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::load] Error loading #{absolute_path}: Expected file_hash of #{content_hash} but found #{json_obj['file_hash']}")
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Error loading #{absolute_path}: Expected file_hash of #{content_hash} but found #{json_obj['file_hash']}")
return nil
end
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::load] Loading #{absolute_path} from cache")
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Loading #{absolute_path} from cache")

json_obj['data']
rescue RuntimeError => e
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::load] Error loading #{absolute_path}: #{e}")
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Error loading #{absolute_path}: #{e}")
raise
end

Expand All @@ -67,9 +67,16 @@ def save(absolute_path, section, content_string)
content['path'] = absolute_path
content['section'] = section

PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.save] Saving #{absolute_path} to cache")
save_file(cache_file, content.to_json)
end

def clear!
return unless active?
PuppetLanguageServerSidecar.log_message(:warn, '[PuppetLanguageServerSidecar::Cache::FileSystem.clear] Filesystem based cache is being cleared')
FileUtils.rm(Dir.glob(File.join(cache_dir, '*')), :force => true)
end

private

def file_key(filepath, section)
Expand Down
4 changes: 4 additions & 0 deletions lib/puppet-languageserver-sidecar/cache/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def load(*)
def save(*)
true
end

def clear!
nil
end
end
end
end
Loading

0 comments on commit ca0175f

Please sign in to comment.